1

I'm using a mapview inside an activity, but I'm receiving a strange null pointer exception that I can't seem to pinpoint the cause of.

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

Then inside the activity:

private MapView mMapView;

initialization in onCreate:

mMapView = (MapView) findViewById(R.id.myMapView);

and overriding the methods:

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();


}

And I'm continually receiving a crash on the mapview onDestroy method whose stacktrace is as follows:

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) 

Given that mmapview is initialized at the start of onCreate from the xml and exists fine up until the onDestroy, what could be the cause of this null pointer?

Jon
  • 7,941
  • 9
  • 53
  • 105

3 Answers3

4

Turns out in the end that the reason was because I had the mapview.oncreate inside an if statement that wasn't being executed. As soon as I moved it to the very beginning of the oncreate method the error stopped occcuring.

Jon
  • 7,941
  • 9
  • 53
  • 105
2

If you are sure it's fine till onDestroy() I suggest doing like this,

@Override
protected void onDestroy() {
    if(mMapView != null) {
        mMapView.onDestroy();
    }
    super.onDestroy();
}
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
0

I don't think you have to worry about calling onDestroy() on your view. I could be wrong but it should be done anyway when the activity is destroyed being a view on the XML.

If you would have other objects (adapters or presenters) you initialized on onCreate() it could be a good idea to clean them up (call release methods/set them to null or whatever is needed)

Apart for that a good rule that I follow is for onPause/onDestroy what Shree Krishna suggests: clean up your resources and then call 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();
}
ctarabusi
  • 343
  • 1
  • 11