I've tried to look this up in other threads and apply the solutions I've found there, to my own issue, but nothing seems to have worked. So here goes:
In one class I have, which creates a new Polygon Overlay:
public void addPolyLines(ArrayList<KrollDict> polyLines){
// Remove the line overlay
List<Overlay> mMapOverlays = view.getOverlays();
boolean rm = mMapOverlays.remove(polyLineOverlay);
polyLineOverlay = new PolygonOverlay(polyLines); // KEY LINE
mMapOverlays.add(polyLineOverlay);
view.invalidate();
}
And these are the guts of my PolygonOverlay class. A concurrent modification exception is thrown at the while(it.hasNext()) line and I can't figure out why. I don't believe I'm modifying the mPolyLines array. drawLines is called from the Overlays native draw method, and sometimes it looks like it's called constantly.
ArrayList<KrollDict> mPolyLines;
public PolygonOverlay(ArrayList<KrollDict> polyLines){
mPolyLines = polyLines;
}
public void drawLines(MapView mv, Canvas canvas) {
Iterator<KrollDict> it = mPolyLines.iterator();
// Go through each line
while(it.hasNext()){// CONCURRENTMODIFICATIONEXCEPTION THROWN HERE
KrollDict kd = it.next();
String[] pointsArr = kd.getStringArray("points");
String color = kd.getString("color");
float width = new Float(kd.getDouble("width")).floatValue();
int alpha = kd.getInt("alpha");
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
Paint paint = new Paint();
paint.setColor(Color.parseColor(color));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(width);
//paint.setAlpha(alpha);
// Loop through the coordinates
for(int i = 0; i< pointsArr.length; i++){
String[] coordinates = convertStringToArray(pointsArr[i]);
Double latitude = new Double(Double.parseDouble(coordinates[3]) * 1E6);
Double longitude = new Double(Double.parseDouble(coordinates[1]) * 1E6);
GeoPoint gp = new GeoPoint(latitude.intValue(), longitude.intValue());
Point point = new Point();
point = mv.getProjection().toPixels(gp, point);
x2 = point.x;
y2 = point.y;
if (i > 0) {
canvas.drawLine(x1, y1, x2, y2, paint);
}
x1 = x2;
y1 = y2;
}
}// while
}