-1

您好,我在 Android 3.2 中收到此错误,它没有指向我的代码,所以我不知道如何修复它

05-07 18:08:36.960: E/AndroidRuntime(12435): FATAL EXCEPTION: main
05-07 18:08:36.960: E/AndroidRuntime(12435): java.util.ConcurrentModificationException
05-07 18:08:36.960: E/AndroidRuntime(12435):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:44)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at com.google.android.maps.MapView.onDraw(MapView.java:530)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.View.draw(View.java:9304)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.drawChild(ViewGroup.java:2586)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.drawChild(ViewGroup.java:2584)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.drawChild(ViewGroup.java:2584)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.drawChild(ViewGroup.java:2584)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.View.draw(View.java:9307)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.widget.FrameLayout.draw(FrameLayout.java:419)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2076)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewRoot.draw(ViewRoot.java:1706)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1420)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.view.ViewRoot.handleMessage(ViewRoot.java:2066)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.os.Looper.loop(Looper.java:132)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at android.app.ActivityThread.main(ActivityThread.java:4126)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at java.lang.reflect.Method.invoke(Method.java:491)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
05-07 18:08:36.960: E/AndroidRuntime(12435):    at dalvik.system.NativeStart.main(Native Method)

但是这是导致错误的代码

protected ItemizedOverlay<OverlayItem> doInBackground(Void... params) {
...
synchronized (this.mapView) {//ConcurrentModificationException
                    if (!this.isCancelled())
                    {

                        for (Object element : this.mapView.getOverlays()) {
                            Overlay o  = (Overlay) element;
                            if (o instanceof GestionaleItemizedOverlay)
                            {
                                this.mapView.getOverlays().remove(o);//this one
                            }

                        }

                        if (overlay.size() > 0)
                        {
                            this.mapView.getOverlays().add(overlay);//or this one
                        }
                    }
                }
...


}

那么我该如何更正我的代码呢?

4

1 回答 1

1

您不能在迭代列表时修改、添加/删除列表。您正在使用的 foreach 循环在后台创建一个Iterator对象。如果您想修改列表,请使用常规 for 循环。

试试下面的,你应该明白了。

List list = this.mapView.getOverlays();
for(int i = 0:i<list.size();i++){
    Overlay o  = (Overlay) list.get(i);
    if (o instanceof GestionaleItemizedOverlay)
    {
        this.mapView.getOverlays().remove(o);//this one
    }
}

是特定于 MapView 覆盖的更准确的解决方案。

于 2012-05-07T16:21:50.170 回答