Android在空对象引用上出现“ boolean com.mapbox.mapboxsdk.maps.Style.isFullyLoaded()”崩溃,

时间:2019-01-20 15:21:02

标签: android maps mapbox mapbox-android

嗨,我正在尝试使用MapBox进行导航,并且像这样崩溃

2019-01-20 19:00:16.331 32005-32005/com.example.mapboxtest E/Mbgl-MapChangeReceiver: Exception in onDidFinishLoadingStyle
android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
    at android.content.res.ResourcesImpl.getValueForDensity(ResourcesImpl.java:225)
    at android.content.res.Resources.getDrawableForDensity(Resources.java:887)
    at android.content.res.Resources.getDrawable(Resources.java:827)
    at android.content.Context.getDrawable(Context.java:626)
    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:463)
    at com.mapbox.mapboxsdk.location.Utils.getDrawable(Utils.java:75)
    at com.mapbox.mapboxsdk.location.LayerBitmapProvider.generateBitmap(LayerBitmapProvider.java:26)
    at com.mapbox.mapboxsdk.location.LocationLayerController.styleForeground(LocationLayerController.java:303)
    at com.mapbox.mapboxsdk.location.LocationLayerController.applyStyle(LocationLayerController.java:109)
    at com.mapbox.mapboxsdk.location.LocationLayerController.initializeComponents(LocationLayerController.java:92)
    at com.mapbox.mapboxsdk.location.LocationLayerController.<init>(LocationLayerController.java:84)
    at com.mapbox.mapboxsdk.location.LocationComponent.initialize(LocationComponent.java:991)
    at com.mapbox.mapboxsdk.location.LocationComponent.activateLocationComponent(LocationComponent.java:292)
    at com.mapbox.services.android.navigation.ui.v5.map.NavigationMapboxMap.initializeLocationComponent(NavigationMapboxMap.java:549)
    at com.mapbox.services.android.navigation.ui.v5.map.NavigationMapboxMap.<init>(NavigationMapboxMap.java:80)
    at com.example.mapboxtest.MainActivity$1.onStyleLoaded(MainActivity.java:45)
    at com.mapbox.mapboxsdk.maps.MapboxMap.notifyStyleLoaded(MapboxMap.java:835)
    at com.mapbox.mapboxsdk.maps.MapboxMap.onFinishLoadingStyle(MapboxMap.java:202)
    at com.mapbox.mapboxsdk.maps.MapView$MapCallback.onDidFinishLoadingStyle(MapView.java:1164)
    at com.mapbox.mapboxsdk.maps.MapChangeReceiver.onDidFinishLoadingStyle(MapChangeReceiver.java:194)
    at com.mapbox.mapboxsdk.maps.NativeMapView.onDidFinishLoadingStyle(NativeMapView.java:979)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:326)
    at android.os.Looper.loop(Looper.java:160)
    at android.app.ActivityThread.main(ActivityThread.java:6892)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)

这些是我现在使用的依赖项

 // MAP BOX
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:7.0.1'
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.27.0'

这是我崩溃的地方

     @Override
    public void onMapReady(@NonNull MapboxMap mapboxMap) {
        mMapboxMap = mapboxMap;
        mapboxMap.setStyle(new Style.Builder().fromUrl(getString(R.string.navigation_guidance)), new Style.OnStyleLoaded() {
            @Override
            public void onStyleLoaded(@NonNull Style style) {
//                enableLocationComponent();
                if (style.isFullyLoaded()) {
                    navigationMap = new NavigationMapboxMap(mapView, mMapboxMap);
//
//                // For Location updates
//                initializeLocationEngine();
//
//                // For navigation logic / processing
//        initializeNavigation(mMapboxMap);
                    navigationMap.updateCameraTrackingMode(NavigationCamera.NAVIGATION_TRACKING_MODE_NONE);
                }
            }
        });
    }

此行引起了问题

navigationMap =新的NavigationMapboxMap(mapView,mMapboxMap);

我检查了此链接https://github.com/mapbox/mapbox-gl-native/wiki/Android-6.x-to-7.x-migration-guide

发现了这样的东西

  

为了避免java.lang.NullPointerException:尝试调用   虚拟方法'布尔值   com.mapbox.mapboxsdk.maps.Style.isFullyLoaded()'在空对象上   参考中提供的样式参数   LocationComponent#activate方法必须为@NonNull并已完全加载。   最好的方法是传递OnStyleLoaded中提供的样式   回调。

但是仍然没有运气。

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并尝试了所有解决方案。我更新到最新的mapbox sdk 8.4.0,并遵循ComponentNavigationActivity#onMapReady,但还是没有运气。

然后我发现我们需要放:

<item name="navigationViewLocationLayerStyle">@style/NavigationLocationLayerStyle</item>

在自定义说明视图样式内,然后像这样在onCreate()内部应用:

setTheme(R.style.customInstructionView);

这解决了问题。希望对您有所帮助!

答案 1 :(得分:0)

https://github.com/mapbox/mapbox-navigation-android/issues/1692通过提供默认值(如果找不到有效样式)来解决此问题。同时,您可以将此代码段添加到style.xml中进行修复:

<item name="navigationViewLocationLayerStyle">@style/NavigationLocationLayerStyle</item>