IllegalStateException映射大小不应为0

时间:2013-07-23 20:40:41

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

我有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.i.b(Unknown Source)
07-23 16:28:42.725: E/AndroidRuntime(19095):    at maps.ah.w.a(Unknown Source)
07-23 16:28:42.725: E/AndroidRuntime(19095):    at maps.ah.s.a(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不为空,所以我不知道导致错误的原因是什么?

1 个答案:

答案 0 :(得分:10)

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个param版本。

请参阅此处的文档:newLatLngBounds

或者,您可以倾听要完成的布局,而不是在onResume中调用此代码。