我的应用程序从后台恢复后,我有时会崩溃:
#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>
答案 0 :(得分:2)
当你的应用程序进入后台时,你需要处理生命周期事件 - 很可能onMapReady在分离片段后被触发。
SupportMapFragment旨在让您从中扩展,如果您不能使用它,您应该看看它如何处理生命周期事件,如onStop和onDestroyView。