从后台恢复我的应用程序后,我有时会崩溃:
#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
...
}
}
平台:安卓7、8
Mapbox SDK 版本: com.mapbox.mapboxsdk:mapbox-android-sdk:5.1.5@aar, com.mapbox.mapboxsdk:mapbox-android-services:2.1.3@aar
触发行为的步骤
- 打开应用
- 稍等片刻...
- 重新打开应用程序<-有时它会崩溃,有时它不会