BlueSun3k1
BlueSun3k1

Reputation: 767

Mapbox SDK - App crashes when using animateCamera during click event

I'm working with Mapbox SDK for Android and I'm doing a simple task of "click and move" but for some reason whenever I click on a location and the camera moves to the location, the app crashes with the following error.

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.mapbox.mapboxsdk.maps.MapboxMap.animateCamera(com.mapbox.mapboxsdk.camera.CameraUpdate, int)' on a null object reference

The error then points to this line.

mapboxMap.animateCamera(CameraUpdateFactory

in

mapboxMap.animateCamera(CameraUpdateFactory
            .newCameraPosition(new CameraPosition.Builder()
                    .target(new LatLng(latLng))
                    .zoom(13)
                    .build()), 1500);

The LatLng is not empty. I made sure to validate if the coordinates are being passed and they are.

As a matter of fact, even if I hardcode the coordinates (lat / lng) it still crashes.

Note: This happens with moveCamera(), easeCamera() and animateCamera().

'void com.mapbox.mapboxsdk.maps.MapboxMap.moveCamera(com.mapbox.mapboxsdk.camera.CameraUpdate)' on a null object reference

'void com.mapbox.mapboxsdk.maps.MapboxMap.easeCamera(com.mapbox.mapboxsdk.camera.CameraUpdate)' on a null object reference

However, one thing to note is that if I place that code in the onMapReady() instance, the map loads with the camera positioned where the coordinates point to but if I perform a click event to trigger the function to move the camera to where I clicked, it crashes.

Another thing I'd like to mention is that when I click the camera actually GOES to the location but it crashes right away. The reason I know this is because when the android studio emulator tries to "recover the app", it shows the map on the location it was meant to go to.

Any ideas why this happens?

other functions like forward and reverse geocoding work just fine when I perform a click event.

Upvotes: 0

Views: 410

Answers (1)

BlueSun3k1
BlueSun3k1

Reputation: 767

The problem I was having which was making the function not recognize the map itself and thus returning NULL was:

@Override
    public void onMapReady(@NonNull final MapboxMap mapboxMap) {
        mapboxMap = mapboxMap; //<=== THIS HERE WAS THE PROBLEM.
                              //It should be this.mapboxMap = mapboxMap

..........

I have the mapboxMap declare as a global in the activity but when the map initialized, it was assigning itself to the global variable and keeping it within the scope of the onMapReady and it was impossible for the function that moves the camera to locate the it all.

in any case, it all works now.

Upvotes: 1

Related Questions