IllegalStateException地图大小不应该为0

我有一个显示地图的MapFragment

如果用户离开活动(或者设备进入睡眠状态),则返回onResume被调用,并将地图移回标记。

但是,当我移动相机,我得到一个例外

 07-23 16:28:42.725: E/AndroidRuntime(19095): java.lang.RuntimeException: Unable to resume activity {ecm2.android/ecm2.android.EMGNoteMapActivity}: java.lang.IllegalStateException: Map size should not be 0. Most likely, layout has not yet occured for the map view. 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.app.ActivityThread.access$600(ActivityThread.java:141) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.os.Handler.dispatchMessage(Handler.java:99) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.os.Looper.loop(Looper.java:137) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.app.ActivityThread.main(ActivityThread.java:5041) 07-23 16:28:42.725: E/AndroidRuntime(19095): at java.lang.reflect.Method.invokeNative(Native Method) 07-23 16:28:42.725: E/AndroidRuntime(19095): at java.lang.reflect.Method.invoke(Method.java:511) 07-23 16:28:42.725: E/AndroidRuntime(19095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 07-23 16:28:42.725: E/AndroidRuntime(19095): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 07-23 16:28:42.725: E/AndroidRuntime(19095): at dalvik.system.NativeStart.main(Native Method) 07-23 16:28:42.725: E/AndroidRuntime(19095): Caused by: java.lang.IllegalStateException: Map size should not be 0. Most likely, layout has not yet occured for the map view. 07-23 16:28:42.725: E/AndroidRuntime(19095): at maps.as.ib(Unknown Source) 07-23 16:28:42.725: E/AndroidRuntime(19095): at maps.ah.wa(Unknown Source) 07-23 16:28:42.725: E/AndroidRuntime(19095): at maps.ah.sa(Unknown Source) 07-23 16:28:42.725: E/AndroidRuntime(19095): at maps.ah.an.b(Unknown Source) 07-23 16:28:42.725: E/AndroidRuntime(19095): at bgc.onTransact(SourceFile:92) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.os.Binder.transact(Binder.java:310) 07-23 16:28:42.725: E/AndroidRuntime(19095): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.animateCamera(Unknown Source) 07-23 16:28:42.725: E/AndroidRuntime(19095): at com.google.android.gms.maps.GoogleMap.animateCamera(Unknown Source) 07-23 16:28:42.725: E/AndroidRuntime(19095): at ecm2.android.Fragments.EMGMap.centerOnNote(EMGMap.java:340) 07-23 16:28:42.725: E/AndroidRuntime(19095): at ecm2.android.Fragments.EMGMap.fetchMapSettings(EMGMap.java:283) 07-23 16:28:42.725: E/AndroidRuntime(19095): at ecm2.android.Fragments.EMGMap.onResume(EMGMap.java:289) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1820) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:412) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.app.Activity.performResume(Activity.java:5195) 07-23 16:28:42.725: E/AndroidRuntime(19095): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732) 07-23 16:28:42.725: E/AndroidRuntime(19095): ... 12 more 

这是我onResume上的相机动作

 if(map != null && northEast != null && southWest != null){ map.animateCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(southWest,northEast),10)); }else if(map != null && centerPoint != null){ CameraPosition position = new CameraPosition.Builder() .target(centerPoint).zoom(17).build(); map.animateCamera(CameraUpdateFactory.newCameraPosition(position)); } 

如果我拿出相机的运动,它不会崩溃,但不会回到他们离开它或什么东西的地步。

我检查,以确保map不为空,所以我不知道是什么原因造成的错误?

Solutions Collecting From Web of "IllegalStateException地图大小不应该为0"

 Caused by: java.lang.IllegalStateException: Map size should not be 0. Most likely, layout has not yet occured for the map view. 

这应该是清楚的。 你不能打电话

 map.animateCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(southWest,northEast),10)); 

在布局完成之前。 您需要使用3参数版本。

请参阅这里的文档: newLatLngBounds

另外,你可以监听布局来完成,而不是在onResume中调用这个代码。