0

I'm getting very weird NullPointerException when trying to get current zoom level from my MapView. The problem is that this occurs not every time and happens somewhere inside getZoomLevel() function. Investigating the source code of this function shows that this is just a getter function and NullPointerException should not potentially happen here. The mapView itself is not null at the time it happens and calls to other MapView interfaces (such as getProjection() work fine).

Here is a brief background of my code structure:

I have map view and ItemizedOverlay with certain amount of items. When I have new data - I need to coalesce items which are close to each other. One of the params to coalesce() function is current zoom level which in theory I should be able to get from my MapView.

I tried to place call to coalesce() inside uiThread but I still get NullPointerException sometimes.

I managed to workaround this issue by keeping track of last known zoomLevel within my custom map view and returning it when getZoomLevel fails:

public final int getCurrentZoomLevel() 
{
    try 
    {
        return this.getZoomLevel();
    } catch (Exception ex) 
    {
        ex.printStackTrace();
        return lastZoomLevel;
    }
}

But still, I'm curious why it happens. It happens on different devices and different Android versions.

Here is stack trace:

07-18 11:31:22.435: W/System.err(8054): java.lang.NullPointerException
07-18 11:31:22.435: W/System.err(8054):     at com.google.android.maps.MapView.getZoomLevel(MapView.java:1094)
07-18 11:31:22.435: W/System.err(8054):     at com.inrix.lib.view.CustomMapView.getCurrentZoomLevel(CustomMapView.java:195)
07-18 11:31:22.435: W/System.err(8054):     at com.inrix.lib.mapitems.incidents.IncidentMapItemOverlay.performCoalesceInternal(IncidentMapItemOverlay.java:109)
07-18 11:31:22.435: W/System.err(8054):     at com.inrix.lib.mapitems.MapItemsOverlay$4.run(MapItemsOverlay.java:448)
07-18 11:31:22.435: W/System.err(8054):     at java.lang.Thread.run(Thread.java:1096)
07-18 11:31:22.435: W/System.err(8054):     at com.inrix.lib.mapitems.MapItemsOverlay.recoalesce(MapItemsOverlay.java:450)
07-18 11:31:22.442: W/System.err(8054):     at com.inrix.lib.mapitems.MapItemsOverlay$2.run(MapItemsOverlay.java:281)
07-18 11:31:22.442: W/System.err(8054):     at android.os.Handler.handleCallback(Handler.java:587)
07-18 11:31:22.442: W/System.err(8054):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 11:31:22.442: W/System.err(8054):     at android.os.Looper.loop(Looper.java:123)
07-18 11:31:22.442: W/System.err(8054):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-18 11:31:22.442: W/System.err(8054):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 11:31:22.442: W/System.err(8054):     at java.lang.reflect.Method.invoke(Method.java:521)
07-18 11:31:22.442: W/System.err(8054):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-18 11:31:22.442: W/System.err(8054):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-18 11:31:22.442: W/System.err(8054):     at dalvik.system.NativeStart.main(Native Method)

Call to coalesce looks like:

protected void performCoalesceInternal(IOnCoalesceCompleteListener callback) 
{
    if (callback != null) 
    {
        callback.onCoalesceComplete(coalescer.coalesce(
                mapItemsController.getRawItemsCollection(), 
                MapItemType.Incident,  
                mapView.getCurrentZoomLevel(), 
                mapView.getProjection()));
    }
}

Please note that I have NullPointerException only on getZoomLevel() call. mapView.getProjection() works fine.

Activity creation/initialization stage:

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ........

    this.setContentView(R.layout.content_inrixmap_activity);

    // Initialize map.
    this.mapView = (CustomMapView) findViewById(R.id.map_view);

    ........

    // Create overlays.
    this.currentLocationOverlay = new CurrentLocationOverlay(this.mapView);
    this.trafficTileOverlay = new TrafficTileOverlay(new TrafficTileManager(this, this.trafficTileHandler));
    this.cameraOverlay = new CameraMapItemOverlay(this, this.mapView, this);
    this.incidentsOverlay = new IncidentMapItemOverlay(this, this.mapView, this);

    // Add overlays to map.
    this.mapView.addOverlay(this.trafficTileOverlay);
    this.mapView.addOverlay(this.currentLocationOverlay);
    this.mapView.addOverlay(this.cameraOverlay);
    this.mapView.addOverlay(this.incidentsOverlay);
    this.mapView.preLoad();
}
2
  • Can you show us the code for when you create your map activity? Jul 18, 2012 at 20:25
  • @BlaineOmega not sure what are you interested in, since activity code is massive and there is no reason to paste all that code in here. However I have updated my question with activity creation/initialization code Jul 18, 2012 at 21:17

2 Answers 2

0

Are you sure that you called populate after adding new data to the Itemized Overlay?

Good luck, Luis

1
  • Yes, I am sure I call populate after I add items to overlay. I just didn't get how populate() and ItemizedOverlay related to NullPointerException in mapView. Overlay should not affect zoom level in any way. Isn't it? Jul 19, 2012 at 5:06
0

Apparently the problem is a bit specific to my code. Apparently, android caches instance of a mapView to be processed by a callback. And when my activity gets recreated, callback uses old instance which is valid, but unitialized.

Solved by cancelling all active network requests when mapView gets recreated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.