Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[android] NPE when invoking isInfoWindowShown() #10390

Closed

Description

@zugaldia

Some users are reporting a NPE on com.mapbox.mapboxsdk.annotations.Marker.isInfoWindowShown().

The stacktrace looks like:

1 com.mapbox.mapboxsdk.maps.AnnotationManager.deselectMarkers(AnnotationManager.java:366)
2 com.mapbox.mapboxsdk.maps.MapGestureDetector$GestureListener.onSingleTapConfirmed(MapGestureDetector.java:323)
3 android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:273)
4 android.os.Handler.dispatchMessage(Handler.java:111)
5 android.os.Looper.loop(Looper.java:194)
6 android.app.ActivityThread.main(ActivityThread.java:5556)
7 java.lang.reflect.Method.invoke(Native Method)
8 java.lang.reflect.Method.invoke(Method.java:372)
9 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967)
10 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

At this point I don't have more information on particular devices, or snippets, able to reproduce this issue.

cc: @tobrun

Activity

  1. added
    Android
    on Nov 6, 2017
  2. tobrun commented on Nov 7, 2017

    @tobrun
    Member

    This shouldn't happen in normal use-cases. If possible please ask the reporting users from some follow up code. For now I can only harden the code, even though this shouldn't happen with normal usage.

  3. added this to the android-v5.2.0 milestone on Nov 7, 2017
  4. lct8712 commented on Nov 8, 2017

    @lct8712

    Hi, I'm the one reporting this issue. We're using mapbox in our app, and this crash occurs 150 times per day, makes it the top one in our crash list.
    Here is some logcat from our users, hope it helps.
    mapbox.zip

  5. tobrun commented on Nov 8, 2017

    @tobrun
    Member

    @lct8712 I would be more interested in seeing how you are selecting these markers or if you call anything to remove or make them null.

  6. lct8712 commented on Nov 9, 2017

    @lct8712

    We use markers a lot, here is the code snippet. I can provide the complete java file if you are interested.

    Add a marker

    mapBoxView.getMapAsync(mapboxMap1 -> {
        MarkerViewOptions options =
            new MarkerViewOptions().position(PositionUtils.gcj_To_Gps84(latitude, longitude))
                .icon(IconFactory.getInstance(mapBoxView.getContext()).fromBitmap(convertViewToBitmap(contentView)))
                .anchor(u, v);
        Marker marker = mapboxMap1.addMarker(options);
        if (isRouteEndPoint) {
            routeEndPointMarker = marker;
        }
    });
    

    Select or click a marker

    public void autoSelectedItemPoi(HeatAreaEntity.HotPoint hotPoint) {
        mapBoxView.getMapAsync(mapboxMap -> {
            double[] coordinates = hotPoint.getLocation().getCoordinates();
            LatLng latLng = PositionUtils.gcj_To_Gps84(coordinates[1], coordinates[0]);
            List<Marker> markers = mapboxMap.getMarkers();
            for (Marker marker : markers) {
                if (latLng.equals(marker.getPosition())) {
                    updateSelectedMarker(marker, hotPoint);
                }
            }
        });
    }
    
    private void markerClick(OnMarkerClickListener onMarkerClickListener, Marker marker) {
        for (HeatAreaEntity.HotPoint hotPoint : CollectionUtils.notNull(lastDataSource)) {
            HeatAreaEntity.Point location = hotPoint.getLocation();
            LatLng latLng = PositionUtils.gcj_To_Gps84(location.getCoordinates()[1], location.getCoordinates()[0]);
            if (latLng.equals(marker.getPosition())) {
                boolean shouldCallback = (lastHotPoint == null || !lastHotPoint.getId().equals(hotPoint.getId()));
                if (shouldCallback) {
                    onMarkerClickListener.action0(hotPoint);
                }
                if (PointType.getType(hotPoint.getPointType()) != PointType.AOI && checkEnableClick(marker)) {
                    updateSelectedMarker(marker, hotPoint);
                }
                break;
            }
        }
    }
    
    private void updateSelectedMarker(Marker marker, HeatAreaEntity.HotPoint hotPoint) {
        if (lastHotPoint != null && lastHotPoint.getId().equals(hotPoint.getId())) {
            return;
        }
        marker.setIcon(
            IconFactory.recreate(marker.getIcon().getId(), convertViewToBitmap(getSelectedView(true, hotPoint))));
        resetLastSelectedMarker();
        lastSelectedMarker = marker;
        lastHotPoint = hotPoint;
    }
    

    Remove markers

    public void clearAllMarker(LocationRawData locationRawData) {
        mapBoxView.getMapAsync(mapboxMap -> {
            LatLng latLng = null;
            if (locationRawData != null) {
                latLng = PositionUtils.gcj_To_Gps84(locationRawData.getLatitude(), locationRawData.getLongitude());
            }
            List<Marker> markers = mapboxMap.getMarkers();
            for (Marker marker : markers) {
                if (!marker.getPosition().equals(latLng)) {
                    mapboxMap.removeMarker(marker);
                }
            }
        });
    }
    
    private void removeMarker(List<HeatAreaEntity.HotPoint> needRemoveHotPointList) {
        mapBoxView.getMapAsync(mapboxMap -> {
            List<Marker> markers = mapboxMap.getMarkers();
            for (HeatAreaEntity.HotPoint hotPoint : needRemoveHotPointList) {
                HeatAreaEntity.Point location = hotPoint.getLocation();
                LatLng latLng = PositionUtils.gcj_To_Gps84(location.getCoordinates()[1], location.getCoordinates()[0]);
                for (Marker marker : markers) {
                    if (latLng.equals(marker.getPosition())) {
                        mapboxMap.removeMarker(marker);
                    }
                }
            }
        });
    }
    
  7. tobrun commented on Nov 9, 2017

    @tobrun
    Member

    Thank you for providing that example code. there are couple of things strange about your setup:

    • you constantly call getMapAsync i every method, please call it once and retain an instance of MapboxMap. Please clarify if there a reason for doing it the way shown above.
    • you are rolling you own selection mechanism it seems? Why not use the internal automatic select mechanisme? I feel most of the code could be simplified with using the provided api and thus also not hitting the exception in OP. In any case, the whole code snippet in Select of click marker can be removed and replaced with listening to MarkerClickListener. Additionally you can control showing an infowindow by returning true/false there. If you want to select a Marker you can hook into`MapboxMap#selectMarker, no need to roll your own system around this.

    Let us know if you have any more questions, in any case the exception from OP will not be shown anymore when #10403 has landed.

  8. Guardiola31337 commented on Nov 17, 2017

    @Guardiola31337
    Contributor

    Hey 👋 @lct8712
    #10403 landed and it's available in 5.2.0-beta.5. Would you be able to retest this with the latest version 5.2.0-beta.5? Are we good to close here?

  9. removed this from the android-v5.2.0 milestone on Nov 17, 2017
  10. tobrun commented on Nov 20, 2017

    @tobrun
    Member

    This landed in 5.2.0 final, closing, feel free to reach out if still an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Android

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions