android:带有地图叠加层的ConcurrentModificationException

时间:2012-01-23 15:23:23

标签: java android google-maps 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();
}

这些是我的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
    }

1 个答案:

答案 0 :(得分:1)

尝试

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

通过制作克隆,您可以安全地反对在迭代时更改列表的人。

相关问题