我在活动中使用了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"
/>
然后在活动内:
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是在xml的onCreate开始时初始化的,并且在onDestroy之前一直存在,可能是这个空指针的原因是什么?
答案 0 :(得分:3)
最终证明原因是因为我在没有执行的if语句中有mapview.oncreate。一旦我将它移动到oncreate方法的最开头,错误就会停止发生。
答案 1 :(得分:1)
如果您确定它onDestroy()
我可以这样做,
@Override
protected void onDestroy() {
if(mMapView != null) {
mMapView.onDestroy();
}
super.onDestroy();
}
答案 2 :(得分:0)
我认为你不必担心在你的视图上调用onDestroy()。 我可能是错的,但无论如何,当活动被销毁为XML视图时,它应该完成。
如果您在onCreate()上初始化了其他对象(适配器或演示者),最好清理它们(调用释放方法/将它们设置为null或其他任何需要)
除此之外,我遵循的一个好规则是onPause / onDestroy什么Shree Krishna建议:清理你的资源,然后打电话给super。
@Override
protected void onPause() {
// clean up your resources in respect to your onResume
...
super.onPause();
}
@Override
protected void onDestroy() {
// clean up your resources in respect of onCreate
...
super.onDestroy();
}