1

我在活動中使用mapview,但我收到一個奇怪的空指針異常,我似乎無法找到原因。Android:奇怪的在MapView onDestroy內的活動

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在的onCreate從XML開始初始化並存在罰款,直到的onDestroy,這個空指針的原因是什麼?

+0

如果你刪除'super.onDestroy();'或者在下面移動這行? –

+0

@ShreeKrishna有趣的想法 - 我會嘗試。謝謝! – Jon

+0

不幸的是,崩潰仍然發生 – Jon

回答

2

最終發現原因是因爲我在沒有執行的if語句中有mapview.oncreate。只要我將它移動到oncreate方法的最開始,錯誤就會停止。

1

如果您確信這是罰款,直到onDestroy()我建議做這樣的,

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

我不認爲你不必擔心你的看法調用的onDestroy()。 我可能是錯的,但是當活動被銷燬爲XML的視圖時,應該這樣做。

如果你有你的onCreate(初始化其他對象(適配器或主持人)),這可能是一個好主意,以清除它們(呼叫釋放方法/將它們設置爲空或需要什麼)

除了因爲我遵循的一個好規則是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(); 
}