0

I have built off of the google api MapView demo.

I've extended the MapView class to create a "onPanListener" similar to: How to catch that map panning and zoom are really finished?

I've also created a GroundOverlay class similar to: Android google maps groundoverlay

I'm struggling to figure out why I sometimes get a ConcurrentModificationException. I suspect it might be because I end up with two timer triggered panlisteneres that execute and cause concurrent access issues on the overlay list. However, I can't tell if that is the problem or not. I have tried adding synchronized (mapview.getOverlay()) {} on any code that removes or adds anything to the overlay list.

I have my stack trace listed below, but I'm not exactly sure what to make of it. It looks like the exception maybe starts in my GroundOverlay class from the draw method (but I'm not sure). When I remove the groundoverlay, I don't think I get the ConcurrentModificationException -- bit tough to tell since it is an intermittent thing.

The stack trace does seem to suggest it is coming from the mapview.draw method. I thought maybe I could override that method in my mapview subclass and try and synchronize that, but unfortunately, draw is final in the parent class. Synchronizing dispatchDraw doesn't seem to help.

Is there a better way to debug something like this? Maybe get some more information about the exception somehow -- just knowing what line of my code throws it would be enormously helpful.

I have tried synchronizing

02-12 10:59:31.995: E/AndroidRuntime(24649): FATAL EXCEPTION: main

02-12 10:59:31.995: E/AndroidRuntime(24649): java.util.ConcurrentModificationException
02-12 10:59:31.995: E/AndroidRuntime(24649):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:44)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at com.google.android.maps.MapView.onDraw(MapView.java:530)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.view.View.draw(View.java:6880)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.view.View.draw(View.java:6883)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.view.View.draw(View.java:6883)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2106)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.view.ViewRoot.draw(ViewRoot.java:1562)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1298)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1911)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.os.Looper.loop(Looper.java:130)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at android.app.ActivityThread.main(ActivityThread.java:3821)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at java.lang.reflect.Method.invokeNative(Native Method)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at java.lang.reflect.Method.invoke(Method.java:507)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 10:59:31.995: E/AndroidRuntime(24649):    at dalvik.system.NativeStart.main(Native Method)

Here is my OnChangeListener. Based on the comment, it sounds like clearing and rewriting the overlays isn't the best option. Maybe, instead of trying to get the onpan event from my MapView class, I should really be getting it from my extended OverlayItem class? I can probably try that.

mMapView.setOnChangeListener(new MyMapView.OnChangeListener() {

        @Override
        public void onChange(Metars metar, Pireps pirep, MyMapView view, GeoPoint newCenter, GeoPoint oldCenter,
                int newZoom, int oldZoom) {

            //metar.addOverlays(view);
            if (view.updatingOverlay == false) {
                view.updatingOverlay = true;
                synchronized (view.getOverlays() ) {
                    view.getOverlays().clear();
                    view.getOverlays().add(view.getRadar());
                }
                metar.addOverlays(view);
                pirep.addOverlays(view);
                view.postInvalidate();
                view.updatingOverlay = false;
            }


        }
    });

}

Here's the code for the xxx.addOverlays(view)

public void addOverlays(MyMapView mapView) {

          // do a bunch of stuff and get overlays ready to add
    List<Overlay> mapOverlays = mapView.getOverlays();

    // add them to my subclass of ItemizedOverlay

    synchronized(mapOverlays) {
    if (itemizedoverlay.size() > 0 )
        mapOverlays.add(itemizedoverlay);
    }

}
Community
  • 1
  • 1
EpicAdv
  • 1,164
  • 1
  • 12
  • 22
  • 1
    Please show your code - not that `ConcurrentModificationException` is slightly confusing, as it doesn't necessarily mean that multiple threads are involved. – Jon Skeet Feb 12 '12 at 19:09
  • Have you seen http://stackoverflow.com/questions/2870743/android-2-1-googlemaps-itemizedoverlay-concurrentmodificationexception already? – Matt Ball Feb 12 '12 at 19:10

1 Answers1

2

After reading through Android 2.1 GoogleMaps ItemizedOverlay ConcurrentModificationException more thoroughly, I came to the conclusion that my issue was the fact that I was launching a new thread for the timer which added and removed overlay items, which would intermittently cause the concurrentacccessexception.

I ended up putting the code modifying the overlays inside an Activity.runOnUiThread(...) and haven't seen the problem since.

Community
  • 1
  • 1
EpicAdv
  • 1,164
  • 1
  • 12
  • 22