代码之家  ›  专栏  ›  技术社区  ›  tenprint

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

  •  0
  • tenprint  · 技术社区  · 6 年前

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

    #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。地图框。mapboxsdk:mapbox android sdk:5.1。5@aar,com。地图框。mapboxsdk:mapbox android服务:2.1。3@aar

    触发行为的步骤

    1. 打开应用程序
    2. 请稍候。。。
    3. 重新打开应用程序(<--有时会崩溃,有时不会
    1 回复  |  直到 6 年前
        1
  •  2
  •   Chris    6 年前

    当你的应用程序进入后台时,你需要处理生命周期事件——很可能是在分离片段后启动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