您好我在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
}
}
}
...
}
那么如何更正我的代码呢?
答案 0 :(得分:1)
在迭代时,您无法修改,添加/删除List。您正在使用的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
}
}
Here是一个更准确的MapView叠加解决方案。