Issue
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?
Solution
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.
Answered By - Jon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.