从后台恢复应用程序后,Mapbox SDK崩溃 - 上下文为空

时间:2018-03-26 19:33:08

标签: android mapbox mapbox-android

我的应用程序从后台恢复后,我有时会崩溃:

#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)

我的代码:

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
    ...   
  }
}

平台: Android 7,8

Mapbox SDK版本: com.mapbox.mapboxsdk:mapbox-android-sdk:5.1.5 @ aar,com.mapbox.mapboxsdk:mapbox-android-services:2.1.3@aar < / p>

触发行为的步骤

  1. 打开应用
  2. 等一会儿......
  3. 重新打开应用&lt; - 有时会崩溃,有时却不会

1 个答案:

答案 0 :(得分:2)

当你的应用程序进入后台时,你需要处理生命周期事件 - 很可能onMapReady在分离片段后被触发。

SupportMapFragment旨在让您从中扩展,如果您不能使用它,您应该看看它如何处理生命周期事件,如onStop和onDestroyView。

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