【问题标题】:mapFragment.getMap () is null when the user do a device screen rotation当用户进行设备屏幕旋转时,mapFragment.getMap() 为 null
【发布时间】:2014-12-17 08:46:08
【问题描述】:

我创建了一个 android 应用程序:

  • 显示谷歌地图
  • 检索用户地理定位并在此地理位置中缩放地图
  • 通过网络服务检索 JSON 并创建一些标记

当用户第一次访问该应用程序时,该应用程序就会工作。

但是当他旋转设备屏幕/改变方向时, 地图无法初始化。

在我的 onResume 方法中,我初始化了 mapFragment,如果它不等于 null,我执行 getMap() 以初始化我的 GoogleMap 地图属性。

public class MapsActivity extends ActionBarActivity implements LocationListener, GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

...

@Override
protected void onResume() {
    Log.d(LOG_TAG, "onResume");
    super.onResume();

    if(googleApiClient != null) {
        googleApiClient.connect();

        mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        if(mapFragment != null) {
            Log.d(LOG_TAG, "mapFragment initialized");
        } else {
            Log.d(LOG_TAG, "error during mapFragment initialization");
        }
        map = mapFragment.getMap();
        if(map != null) {
            Log.d(LOG_TAG, "map initialized");
        } else {
            Log.d(LOG_TAG, "error during map initialization");
        }

    }
    Log.d(LOG_TAG, "end of onResume");
}

当用户访问活动时,它正在工作:

12-17 09:35:02.306  25099-25099/com.xxx.xx D/LogMe﹕ onCreate
12-17 09:35:03.746  25099-25099/com.xxx.xx D/LogMe﹕ end of onCreate
12-17 09:35:03.748  25099-25099/com.xxx.xx D/LogMe﹕ onResume
12-17 09:35:03.759  25099-25099/com.xxx.xx D/LogMe﹕ mapFragment initialized
12-17 09:35:03.760  25099-25099/com.xxx.xx D/LogMe﹕ map initialized
12-17 09:35:03.760  25099-25099/com.xxx.xx D/LogMe﹕ end of onResume
12-17 09:35:03.844  25099-25099/com.xxx.xx D/LogMe﹕ onConnected
12-17 09:35:03.848  25099-25099/com.xxx.xx D/LogMe﹕ google play services available
12-17 09:35:03.850  25099-25099/com.xxx.xx D/LogMe﹕ setUpMapIfNeeded
12-17 09:35:03.850  25099-25099/com.xxx.xx D/LogMe﹕ we have a map
12-17 09:35:03.865  25099-25099/com.xxx.xx D/LogMe﹕ setUpMap
12-17 09:35:03.866  25099-25099/com.xxx.xx D/LogMe﹕ location: Location[mProvider=fused,mTime=1418805217845,mLatitude=43.6137752,mLongitude=1.4302528,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=true,mAccuracy=52.5,mExtras=null]
12-17 09:35:03.867  25099-25099/com.xxx.xx D/LogMe﹕ we retrieve markers thx to the web service

但是当他旋转设备屏幕时:

12-17 09:35:25.118  25099-25099/com.xxx.xx D/LogMe﹕ onSaveInstanceState
12-17 09:35:25.119  25099-25099/com.xxx.xx D/LogMe﹕ end of onSaveInstanceState
12-17 09:35:25.260  25099-25099/com.xxx.xx D/LogMe﹕ onCreate
12-17 09:35:25.427  25099-25099/com.xxx.xx D/LogMe﹕ lat et lng: lat/lng: (43.613774948887695,1.4302530884742737)
12-17 09:35:25.428  25099-25099/com.xxx.xx D/LogMe﹕ zoom: 13.0
12-17 09:35:25.430  25099-25099/com.xxx.xx D/LogMe﹕ markers retrieved
12-17 09:35:25.462  25099-25099/com.xxx.xx D/LogMe﹕ end of onCreate
12-17 09:35:25.470  25099-25099/com.xxx.xx D/LogMe﹕ onResume
12-17 09:35:25.609  25099-25099/com.xxx.xx D/LogMe﹕ mapFragment initialized
12-17 09:35:25.609  25099-25099/com.xxx.xx D/LogMe﹕ error during map initialization
12-17 09:35:25.609  25099-25099/com.xxx.xx D/LogMe﹕ end of onResume
12-17 09:35:25.732  25099-25099/com.xxx.xx D/LogMe﹕ onConnected
12-17 09:35:25.735  25099-25099/com.xxx.xx D/LogMe﹕ google play services available
12-17 09:35:25.736  25099-25099/com.xxx.xx D/LogMe﹕ setUpMapIfNeeded
12-17 09:36:28.522  25099-25099/com.xxx.xx D/LogMe﹕ onSaveInstanceState

地图无法初始化。

你有什么想法吗?

谢谢

【问题讨论】:

  • 你不应该使用getMapAsync吗? getMap 方法已弃用。

标签: android google-maps rotation google-maps-android-api-2 android-maps-v2


【解决方案1】:

您可以尝试通过这种方式获取地图:

 protected void onResume() {
        if (mMap != null) {
            // Do markers
            return;
        }
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        if (mMap == null) {
            return;
        }
 }

还有一个名为 hello-map here 的示例项目。 map rotation. 显示没有问题

【讨论】:

  • 嗨?我之前尝试过这个解决方案来创建stackoverflow问题,我也是同样的问题。谢谢我的解决方案是创建一个类 RetainMapFragment 并且它有效。
【解决方案2】:

避免使用 XML 来构建 mapView/SupportMapFragment。使用 FrameLayout,用 SupportMapFragment 的新实例进行替换。你可以用它做更多事情(添加 ChildFragmentManager 等等...)。

private MapView mMapView;
private SupportMapFragment mSupportMapFragment;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    createMapFragmentIfNeeded();
}

@Override
public void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}


private void createMapFragmentIfNeeded() {

    mFragmentManager = getChildFragmentManager();

    if (mSupportMapFragment == null) {

        mSupportMapFragment = createMapFragment();
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.map_view, mSupportMapFragment);
        fragmentTransaction.commit();

    }
}

public void  setUpMapIfNeeded() {
    if (mMap == null && mSupportMapFragment != null) {
        mMap = mSupportMapFragment.getMap();
        if (mMap != null) {
            //Setup map
        }
    }
}

protected SupportMapFragment createMapFragment() {
    try {
        return SupportMapFragment.class.newInstance();
    } catch (java.lang.InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}

【讨论】:

  • 在我的项目中,我不想使用片段,但感谢您的回答:-)。
【解决方案3】:

我已经用创建的新类 RetainMapFragment 解决了我的问题:

import android.os.Bundle;

import com.google.android.gms.maps.MapFragment;

public class RetainMapFragment extends MapFragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
}

}

我在布局中使用了这个新类:

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="3"
    android:layout_margin="5px"
    class="com.xxx.xxx.RetainMapFragment"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    相关资源
    最近更新 更多