我試圖在其他線程中查找這個,並將我在那裏找到的解決方案應用到我自己的問題,但似乎沒有任何工作。所以這裏有雲:android:ConcurrentModificationException與地圖疊加
在一堂課上我有,這將創建一個新的多邊形覆蓋:
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();
}
而這些都是我PolygonOverlay類的膽量。 while(it.hasNext())行引發併發修改異常,我找不到原因。我不相信我正在修改mPolyLines數組。 drawLines是從Overlays原生繪製方法中調用的,有時它看起來像是不斷調用的。
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
}
看到這個線程http://stackoverflow.com/questions/1775717/explain-synchronization-of-collections-when-iterators-are-used – aviad
我看到沒有錯你已發佈的代碼。猜測是瘋狂的,但也許你認爲覆蓋是迭代它自己的ArrayList的私人副本,並且你正在其他地方(可能在另一個線程)更新單獨的副本,而它們實際上是同一個對象。那可能嗎? –
這完全有可能。我從另一個地方添加/刪除的另一個地方是另一個類,它在另一個線程中,但只有當用戶按下添加/刪除按鈕時纔會發生,之後所有這些代碼都會被調用。你認爲這可能嗎?我假設他們被稱爲一個接一個...... – Leonidas