Android 2.1 GoogleMaps ItemizedOverlay ConcurrentModificationException

I can not understand the origin of ConcurrentModificationException . In my activity I call updateMapOverlay() . I also call updateMapOverlay() inside another thread (a TimerTask ), which is called at regular intervals. I take the appropriate locks when calling updateMapOverlay() from both threads. Is this problem caused by what I call updateMapOverlay from a thread other than the UI (i.e. TimerTask ). Has anyone else encountered a similar problem?

private void updateMapOverlay() {

  this.itemizedOverlay.refreshItems(createOverlayItemsList()); List<Overlay> overlays = mapView.getOverlays(); overlays.clear(); overlays.add(cotItemizedOverlay); this.mapview.invalidate(); } 

Thanks.

Exception :

 W/dalvikvm(10641): threadid=3: thread exiting with uncaught exception (group=0x4001b180) E/AndroidRuntime(10641): Uncaught handler: thread main exiting due to uncaught exception E/AndroidRuntime(10641): java.util.ConcurrentModificationException E/AndroidRuntime(10641): at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:64) E/AndroidRuntime(10641): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:41) E/AndroidRuntime(10641): at com.google.android.maps.MapView.onDraw(MapView.java:494) E/AndroidRuntime(10641): at android.view.View.draw(View.java:6535) E/AndroidRuntime(10641): at android.view.ViewGroup.drawChild(ViewGroup.java:1531) E/AndroidRuntime(10641): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) E/AndroidRuntime(10641): at android.view.ViewGroup.drawChild(ViewGroup.java:1529) E/AndroidRuntime(10641): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) E/AndroidRuntime(10641): at android.view.View.draw(View.java:6538) E/AndroidRuntime(10641): at android.widget.FrameLayout.draw(FrameLayout.java:352) E/AndroidRuntime(10641): at android.view.ViewGroup.drawChild(ViewGroup.java:1531) E/AndroidRuntime(10641): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) E/AndroidRuntime(10641): at android.view.ViewGroup.drawChild(ViewGroup.java:1529) E/AndroidRuntime(10641): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258) E/AndroidRuntime(10641): at android.view.View.draw(View.java:6538) E/AndroidRuntime(10641): at android.widget.FrameLayout.draw(FrameLayout.java:352) E/AndroidRuntime(10641): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830) E/AndroidRuntime(10641): at android.view.ViewRoot.draw(ViewRoot.java:1349) E/AndroidRuntime(10641): at android.view.ViewRoot.performTraversals(ViewRoot.java:1114) E/AndroidRuntime(10641): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633) E/AndroidRuntime(10641): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime(10641): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime(10641): at android.app.ActivityThread.main(ActivityThread.java:4363) E/AndroidRuntime(10641): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(10641): at java.lang.reflect.Method.invoke(Method.java:521) E/AndroidRuntime(10641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) E/AndroidRuntime(10641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) E/AndroidRuntime(10641): at dalvik.system.NativeStart.main(Native Method) I/Process ( 95): Sending signal. PID: 10641 SIG: 3 
+1
android concurrency google-maps android-mapview
source share
4 answers

This problem is caused by the fact that I call updateMapOverlay from inside a non-user thread

Yes.

I also call updateMapOverlay () inside another thread (TimerTask) which is called at regular intervals.

Why are you constantly deleting and adding overlays? Just refresh the overlay and invalidate. See here for an example of updating an asynchronous overlay.

+5
source share

Thanks for your reply.

However, in your code you are not doing something like this ( map.getOverlays().remove(sites); and map.getOverlays().add(sites); )?

class OverlayTask extends AsyncTask { @Override public void onPreExecute() { if (sites!=null) { map.getOverlays().remove(sites); map.invalidate(); sites=null; } }

  @Override public Void doInBackground(Void... unused) { SystemClock.sleep(5000); // simulated work sites=new SitesOverlay(); return(null); 

}

  @Override
 public void onPostExecute (Void unused) {
   map.getOverlays (). add (sites);
   map.invalidate ();
  }
 }
+1
source share

Generally speaking, you should be safe if you are only modifying your ItemizedOverlay list / signature map in the user interface stream.

As Mark pointed out, AsyncTask:

 @Override protected void onPostExecute(Cursor cursor) { // modify List/Map populate(); mapView.invalidate(); } 

always executed in the user interface thread, so modifications are safe here.

+1
source share

I have the same problem. To answer your question: β€œWhy are you deleting and adding overlays all the time?”, I do this because some of my overlay elements may no longer exist, or new ones may appear, and existing ones may change the location, depending on what happens in the rest of my specific application. Also, I did not find a way to change the location of OverlayItem without expanding it so that I can do it.

In addition, will any elements in the detailing of the overlay also change the reason for the exclusion of parallel modification?

0
source share

All Articles