LocationComponent com.mapbox.mapboxsdk.maps.MapboxMap.getLocationComponent()' 在空 object 引用上

LocationComponent com.mapbox.mapboxsdk.maps.MapboxMap.getLocationComponent()' on a null object reference

嘿,我正在做一个 mapbox 优步应用程序。骑手和 driver 应用程序曾经正常工作,但在过去的几天里,我在 LocationCompoment 上遇到了一个空错误。你们能帮我理解发生了什么吗? 这是代码:

private void enableLocationComponent(@NonNull Style loadedMapStyle) {

        // Check if permissions are enabled and if not request
        if (PermissionsManager.areLocationPermissionsGranted(this)) {
    
            // Get an instance of the component
            LocationComponent locationComponent = mapboxMap.getLocationComponent();
                         if (locationComponent != null){
            // Activate with options
            locationComponent.activateLocationComponent(
LocationComponentActivationOptions.builder(this, loadedMapStyle).build());
    
            // Enable to make component visible
            locationComponent.setLocationComponentEnabled(true);
    
            // Set the component's camera mode
            locationComponent.setCameraMode(CameraMode.TRACKING);
    
            // Set the component's render mode
            locationComponent.setRenderMode(RenderMode.COMPASS);
       } } else {
            permissionsManager = new PermissionsManager(this);
            permissionsManager.requestLocationPermissions(this);
        }
    }

为什么你有两个 if (PermissionsManager.areLocationPermissionsGranted(this)) 支票?你只需要一个。

设置您的项目以匹配 https://docs.mapbox.com/android/maps/examples/show-a-users-location/,这是显示设备位置的最基本方式。

您还可以在 LocationComponent locationComponent = mapboxMap.getLocationComponent();

之后进行空检查