-1

Hello i get this error in Android 3.2 and it doesn't point to my code, so i don't know how to fix it

05-07 18:08:36.960: E/AndroidRuntime(12435): FATAL EXCEPTION: main
05-07 18:08:36.960: E/AndroidRuntime(12435): java.util.ConcurrentModificationException
05-07 18:08:36.960: E/AndroidRuntime(12435):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:44)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at com.google.android.maps.MapView.onDraw(MapView.java:530)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.View.draw(View.java:9304)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.drawChild(ViewGroup.java:2586)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.drawChild(ViewGroup.java:2584)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.drawChild(ViewGroup.java:2584)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.drawChild(ViewGroup.java:2584)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.View.draw(View.java:9307)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.widget.FrameLayout.draw(FrameLayout.java:419)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2076)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewRoot.draw(ViewRoot.java:1706)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1420)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewRoot.handleMessage(ViewRoot.java:2066)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.os.Looper.loop(Looper.java:132)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.app.ActivityThread.main(ActivityThread.java:4126)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at java.lang.reflect.Method.invoke(Method.java:491)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at dalvik.system.NativeStart.main(Native Method)

however this is the code that causes the error

protected ItemizedOverlay<OverlayItem> doInBackground(Void... params) {
...
synchronized (this.mapView) {//ConcurrentModificationException
                    if (!this.isCancelled())
                    {

                        for (Object element : this.mapView.getOverlays()) {
                            Overlay o  = (Overlay) element;
                            if (o instanceof GestionaleItemizedOverlay)
                            {
                                this.mapView.getOverlays().remove(o);//this one
                            }

                        }

                        if (overlay.size() > 0)
                        {
                            this.mapView.getOverlays().add(overlay);//or this one
                        }
                    }
                }
...


}

so how can i correct my code ?

max4ever
  • 11,909
  • 13
  • 77
  • 115
  • i think you should use `synchronized` because `List` is not synchronized – Samir Mangroliya May 07 '12 at 16:29
  • i am using syncronized(this.mapView) isn't it enough ? plus this.mapView.getOverlays() says Returns: The list of overlays. This has been run through Collections.synchronizedList(java.util.List); thus feel free to query it or modify it as you see fit and the changes will be reflected on the next draw or event. However, if you iterate over it, the entire loop should be enclosed in a block synchronizing on the list. – max4ever May 07 '12 at 16:31

1 Answers1

1

You cannot modify, add/remove, a List while iterating through it. The foreach loop you are using creates an Iterator object in the background. Use a regular for loop if you'd like to modify the list.

Try the following, you should get the idea.

List list = this.mapView.getOverlays();
for(int i = 0:i<list.size();i++){
    Overlay o  = (Overlay) list.get(i);
    if (o instanceof GestionaleItemizedOverlay)
    {
        this.mapView.getOverlays().remove(o);//this one
    }
}

Here is a more accurate solution specific to MapView overlays.

Community
  • 1
  • 1
user845279
  • 2,794
  • 1
  • 20
  • 38
  • i commented the .remove, and noticed that the .add too causes this error – max4ever May 07 '12 at 16:26
  • There must be another `Iterator` that has a lock on the backing list. Please post more code, or look through the execution. Look at [this](http://stackoverflow.com/questions/6294061/java-util-concurrentmodificationexception) first. – user845279 May 07 '12 at 16:30
  • Apparently this issue happens to a lot of people, please search through [stackoverflow](http://stackoverflow.com/questions/9251761/mapview-concurrentmodificationexception-when-updating-overlays). Basically, the UI thread has an `Iterator` lock on the overlay list and you are attempting to modify the list in a separate thread. – user845279 May 07 '12 at 16:34
  • i moved both the remove() and the add() in onPostExecute and it worked, thanks this was right http://stackoverflow.com/questions/9251761/mapview-concurrentmodificationexception-when-updating-overlays – max4ever May 07 '12 at 16:43