【发布时间】:2016-08-09 01:41:12
【问题描述】:
我在 Activity 中使用 mapview,但收到一个奇怪的空指针异常,我似乎无法查明原因。
xml:
<com.google.android.gms.maps.MapView
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/myMapView"
android:layout_width="match_parent"
android:layout_height="156dp"
map:cameraZoom="16"
map:liteMode="true"
android:visibility="invisible"
/>
然后在activity里面:
private MapView mMapView;
在 onCreate 中初始化:
mMapView = (MapView) findViewById(R.id.myMapView);
并覆盖方法:
mMapView.onCreate(savedInstanceState);
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
我不断收到 mapview onDestroy 方法的崩溃,其堆栈跟踪如下:
java.lang.RuntimeException: Unable to destroy activity {myActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.LinkedList.isEmpty()' on a null object reference
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3831)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3849)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.LinkedList.isEmpty()' on a null object reference
at com.google.android.gms.dynamic.zza.zzeJ(Unknown Source)
at com.google.android.gms.dynamic.zza.onDestroy(Unknown Source)
at com.google.android.gms.maps.MapView.onDestroy(Unknown Source)
at myActivity.onDestroy(myActivity.java:454)
at android.app.Activity.performDestroy(Activity.java:6422)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1142)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3818)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3849)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
鉴于 mmapview 在 onCreate 开始时从 xml 初始化并且在 onDestroy 之前一直存在,这个空指针的原因可能是什么?
【问题讨论】:
-
如果您删除
super.onDestroy();或将此行移到下方会怎样? -
@ShreeKrishna 有趣的想法 - 我会试试的。谢谢!
-
不幸的是,崩溃仍然发生
-
您是否尝试过检查是否不为空,如答案?
-
@ShreeKrishna 是的,我有 - 这是我第一次开始遇到错误时尝试的第一件事
标签: android google-maps nullpointerexception google-maps-android-api-2 android-mapview