1

私はアクティビティ内でmapviewを使用していますが、原因を特定できないような奇妙なnullポインタ例外を受け取りました。Android:MapView onDestroyの奇妙なnpeアクティビティ内

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" 
        /> 

そして活性内側:のonCreateで

private MapView mMapView; 

初期化:

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メソッドでクラッシュを受けています:、mmapviewは、XMLからのonCreateの開始時に初期化され、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)  

このnullポインタの原因は何ですか?

+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(上の初期化、他のオブジェクト(アダプターまたはプレゼンター)を持っている希望の場合)には、それらをクリーンアップするために良いアイデアかもしれない(呼解放方法/ nullまたは何でも必要とされているためにそれらを設定)

別に

そのために私が従うべき良いルールは、Shree Krishnaが提案しているonPause/onDestroyです:あなたのリソースを整理し、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(); 
}