Android 当用户执行设备屏幕旋转时,mapFragment.getMap()为空
我创建了一个android应用程序,它:Android 当用户执行设备屏幕旋转时,mapFragment.getMap()为空,android,google-maps,rotation,google-maps-android-api-2,android-maps-v2,Android,Google Maps,Rotation,Google Maps Android Api 2,Android Maps V2,我创建了一个android应用程序,它: 显示谷歌地图 检索用户地理位置并在此地理位置缩放地图 通过web服务检索JSON并创建一些标记 当用户第一次访问该应用程序时,该应用程序就会工作 但当他旋转设备屏幕/改变方向时, 无法初始化映射 在onResume方法中,我初始化了mapFragment,如果它不等于null,则执行getMap()以初始化GoogleMap属性 public class MapsActivity extends ActionBarActivity implement
- 显示谷歌地图
- 检索用户地理位置并在此地理位置缩放地图
- 通过web服务检索JSON并创建一些标记
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
无法初始化映射
你有主意吗
谢谢您可以尝试通过以下方式获取地图:
protected void onResume() {
if (mMap != null) {
// Do markers
return;
}
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap == null) {
return;
}
}
另外,还有一个名为
hellomap
的示例项目。它显示地图旋转没有问题。避免使用XML构建mapView/SupportMapFragment。使用FrameLayout,使用SupportMapFragment的新实例进行替换。您可以用它做更多的事情(添加ChildFragmentManager和更多…)
我已使用创建的新类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"/>
在我的项目中,我不想使用片段,但感谢您的回答:-)。您好?我曾经尝试过这个解决方案来创建stackoverflow问题,我也遇到过同样的问题。谢谢,我的解决方案是创建一个classe RetainmpFragment,它成功了。你不应该使用它吗?getMap
方法已被弃用。
<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"/>