2012-01-23 2 views
1

다른 스레드에서이 내용을 조사하고 거기에서 발견 한 해결책을 내 문제에 적용하려했지만 아무 것도 효과가없는 것 같습니다. 그래서 여기 간다 :android :지도 오버레이가있는 ConcurrentModificationException

새로운 다각형 오버레이를 생성 클래스 I는 한 번에 :

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는 오버레이 네이티브 그리기 메서드에서 호출되며 때로는 끊임없이 호출되는 것처럼 보입니다.

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 
    } 
+0

이 스레드를 참조하십시오. http://stackoverflow.com/questions/1775717/explain-synchronization-of-collections-when-iterators-are-used – aviad

+0

게시 한 코드에는 아무런 문제가 없습니다. 격렬하게 추측하지만 어쩌면 오버레이가 ArrayList 의 자체 복사본이기 때문에 반복적으로 생각하고 있습니다. 다른 개체 (다른 스레드의 개체)를 다른 개체로 업데이트하는 경우도 있습니다. 실제로는 동일한 개체입니다. 그게 가능하니? –

+0

그건 완전히 가능합니다. 해당 ArrayList에서 추가/제거하는 유일한 다른 장소는 다른 클래스의 다른 클래스에 있지만, 사용자가 추가/제거 버튼을 누르면이 코드가 모두 호출됩니다. 그게 될 수 있다고 생각하니? 나는 그들이 순차적으로 호출되었다고 생각했다 ... – Leonidas

답변

1

복제를함으로써

public PolygonOverlay(ArrayList<KrollDict> polyLines){ 
    mPolyLines = (ArrayList<KrollDict>)polyLines.clone(); 
} 

을 시도, 당신은 당신이 그것을 반복하는 동안 목록을 변경하는 사람에 대한 안전합니다.

+1

복사 생성자를 사용한다 :'mPolyLines = new ArrayList (polyLines)', 캐스트를 저장한다. – Matt

+0

굉장 해요, 지금 당장 시험해 보겠습니다! – Leonidas

+0

이봐,이게 지금처럼 작동하는 것 같아. 감사! – Leonidas

관련 문제