java.util.ConcurrentModificationException绘制路径

时间:2012-07-08 18:26:50

标签: android google-maps map routes

我正在使用代码在MapActivity中绘制路线:

ArrayList<Routemark> route_marks = navSet.getRoutemarks();

    for(int i = 2; i < route_marks.size() - 1; i++)
    {
        try
        {

            DirectionPathOverlay dpo = new DirectionPathOverlay(point1, point2);    
            mMapView01.getOverlays().add(dpo);

                point1 = point2;
            lon_d = Double.parseDouble(route_marks.get(i).getLongitude()) * 1E6;
            lon = (int)lon_d;
            lat_d = Double.parseDouble(route_marks.get(i).getLatitude()) * 1E6;
            lat = (int)lat_d;
            point2 = new GeoPoint(lat, lon);
            list_points.add(point2);

        }
        catch(Exception e) {
          }


    }
    mMapView01.getOverlays().add(new DirectionPathOverlay(point2, point2));

public class DirectionPathOverlay extends Overlay {

private GeoPoint gp1;
private GeoPoint gp2;

public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) {
    this.gp1 = gp1;
    this.gp2 = gp2;
}

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
        long when) {
    // TODO Auto-generated method stub
    Projection projection = mapView.getProjection();
    if (shadow == false) {

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Point point = new Point();
        projection.toPixels(gp1, point);
        paint.setColor(Color.BLUE);
        Point point2 = new Point();
        projection.toPixels(gp2, point2);
        paint.setStrokeWidth(2);
        canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
                (float) point2.y, paint);
    }
    return super.draw(canvas, mapView, shadow, when);
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    // TODO Auto-generated method stub

    super.draw(canvas, mapView, shadow);
}

}

我正在将代码执行到新线程中并且我得到了exeption:

    07-08 18:23:21.179: E/AndroidRuntime(23510): FATAL EXCEPTION: main
07-08 18:23:21.179: E/AndroidRuntime(23510): java.util.ConcurrentModificationException
07-08 18:23:21.179: E/AndroidRuntime(23510):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:44)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at com.google.android.maps.MapView.onDraw(MapView.java:530)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.View.draw(View.java:11082)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewGroup.drawChild(ViewGroup.java:2991)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2593)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewGroup.drawChild(ViewGroup.java:2989)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2593)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewGroup.drawChild(ViewGroup.java:2989)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2593)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewGroup.drawChild(ViewGroup.java:2989)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2593)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.View.draw(View.java:11085)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.widget.FrameLayout.draw(FrameLayout.java:462)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2145)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2026)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.os.Looper.loop(Looper.java:137)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at android.app.ActivityThread.main(ActivityThread.java:4441)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at java.lang.reflect.Method.invokeNative(Native Method)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at java.lang.reflect.Method.invoke(Method.java:511)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-08 18:23:21.179: E/AndroidRuntime(23510):    at dalvik.system.NativeStart.main(Native Method)

问题出在哪里?

1 个答案:

答案 0 :(得分:2)

当您尝试在遍历它时修改ConcurrentModificationException时,通常会抛出

List。如果您需要实现此类功能,则应使用CopyOnWriteArrayList而不是简单的ArrayList。希望这会有所帮助。