Android:activity 内 MapView onDestroy 上的奇怪 npe

Android: strange npe on MapView onDestroy inside an activity

我在 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 是在 xml 的 onCreate 开始时初始化的,并且在 onDestroy 之前一直存在,这个空指针的原因可能是什么?

如果您确定 onDestroy() 之前都可以,我建议您这样做,

@Override
protected void onDestroy() {
    if(mMapView != null) {
        mMapView.onDestroy();
    }
    super.onDestroy();
}

我认为您不必担心在您的视图上调用 onDestroy()。 我可能是错的,但是当 activity 作为 XML.

上的视图被销毁时无论如何都应该这样做

如果您有在 onCreate() 上初始化的其他对象(适配器或演示器),清理它们可能是个好主意(将它们释放 methods/set 为 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();
}

最后发现原因是因为我在没有执行的 if 语句中有 mapview.oncreate。一旦我将它移到 oncreate 方法的开头,错误就停止了。