Search code examples
androidgoogle-mapsandroid-3.0-honeycombconcurrentmodification

ConcurrentModification exception in honeycomb


I'm getting this error while working on map (Drawing line on map using GetDirection API). I have used CopyOnWriteArrayList still it sometimes throws ConcurrentModification exception.

CopyOnWriteArrayList<GeoPoint> pointArray;
pointArray =  parcer.getDirectionParcer(jsonObject);

GeoPoint gp1;
GeoPoint gp2 = src;
Iterator<GeoPoint> it1 = pointArray.iterator();

//for(int i=0;i<pointArray.size();i++) // the last one would be crash

Utility.debugger("2");
while (it1.hasNext()) {
    try {
        gp1 = gp2;
        gp2 = (GeoPoint) it1.next();
        mMapView.getOverlays().add(new MyOverLay(gp1,gp2,2,color));
    } catch (ConcurrentModificationException e) {
        Utility.debugger("exception");
        e.printStackTrace();
    }
}

It gives error in it1.next().


Solution

  • Are you calling this code in a non UI thread?

    The ConcurrentModificationException may be due to adding overlays in an Non UI thread, while the UI thread is trying to access the overlays. You can only modify overlays on the UI thread.