简体   繁体   中英

Crash in Mapbox SDK after resuming app from background - context is null

After my app is resumed from the background, I am sometimes getting a crash:

#0. Crashed: main
       at com.mapbox.mapboxsdk.annotations.IconFactory.getInstance(IconFactory.java:53)
       at com.mydomain.myapp.manager.MapManager.(MapManager.java:68)
       at com.mydomain.myapp.fragment.HomeFragment.onMapReady(HomeFragment.java:547)
       at com.mapbox.mapboxsdk.maps.MapView$MapCallback.onMapReady(MapView.java:943)
       at com.mapbox.mapboxsdk.maps.MapView$MapCallback.access$1000(MapView.java:908)
       at com.mapbox.mapboxsdk.maps.MapView$MapCallback$1.run(MapView.java:926)
       at android.os.Handler.handleCallback(Handler.java:790)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6494)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

--

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
       at com.mapbox.mapboxsdk.annotations.IconFactory.getInstance(IconFactory.java:53)
       at com.mydomain.myapp.manager.MapManager.(MapManager.java:68)
       at com.mydomain.myapp.fragment.HomeFragment.onMapReady(HomeFragment.java:547)
       at com.mapbox.mapboxsdk.maps.MapView$MapCallback.onMapReady(MapView.java:943)
       at com.mapbox.mapboxsdk.maps.MapView$MapCallback.access$1000(MapView.java:908)
       at com.mapbox.mapboxsdk.maps.MapView$MapCallback$1.run(MapView.java:926)
       at android.os.Handler.handleCallback(Handler.java:790)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6494)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

My code:

public class HomeFragment implements OnMapReadyCallback {

  MapView mapView;

  @Override View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ...
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);
    ...
  }

  ...

  @Override public void onMapReady(MapboxMap mapboxMap) {
    ...
    IconFactory iconFactory = IconFactory.getInstance(getContext()); <-- ERROR: CONTEXT IS NULL
    ...   
  }
}

Platform: Android 7, 8

Mapbox SDK version: com.mapbox.mapboxsdk:mapbox-android-sdk:5.1.5@aar, com.mapbox.mapboxsdk:mapbox-android-services:2.1.3@aar

Steps to trigger behavior

  1. Open app
  2. Wait a while...
  3. Re-open app <-- sometimes it crashes, sometimes it doesn't

You need to handle the lifecycle events when your app goes into the background - most likely onMapReady is being fired after the Fragment has been detached.

The SupportMapFragment is designed for you to extend from it, if you can't use this you should look at how it handles lifecycle events such as onStop and onDestroyView.

https://github.com/mapbox/mapbox-gl-native/blob/da7759a273c096117e513b249741355865f9af8e/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM