简体   繁体   English

从ArrayList中删除标记:java.util.ConcurrentModificationException

[英]Remove the marker from the ArrayList: java.util.ConcurrentModificationException

I have a checkboxlist where the user can select some routes then a reponse is being getting from the server. 我有一个复选框列表,用户可以在其中选择一些路由,然后从服务器获取响应。 I have method gotoLocation to upadte the location of the markers as well to add a new marker in the map when a new one is being inserted into the table on the serverside with the same route. 我有方法gotoLocation来更新标记的位置,以及在将新标记插入到具有相同路由的服务器端表中时在地图中添加新标记。

I tried it before with HashMap<Integer, Marker> as <id, marker> but I had problem to add a new marker of the new request to the map so I tried it with ArrayList<Integer, String> and it works but I am getting now the error below when I try to remove a marker from the map. 我之前使用HashMap<Integer, Marker> as <id, marker>进行了尝试,但是在将新请求的新标记添加到地图时遇到了问题,所以我使用ArrayList<Integer, String>进行了尝试ArrayList<Integer, String>但是我可以现在,当我尝试从地图上删除标记时,出现以下错误。

How can I fix it? 我该如何解决?

Error: 错误:

07-16 00:19:05.663: E/AndroidRuntime(29919): FATAL EXCEPTION: main
07-16 00:19:05.663: E/AndroidRuntime(29919): Process: com.bustracker, PID: 29919
07-16 00:19:05.663: E/AndroidRuntime(29919): java.util.ConcurrentModificationException
07-16 00:19:05.663: E/AndroidRuntime(29919):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at com.bustracker.Map.gotoLocation(Map.java:132)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at com.bustracker.Map.onNewIntent(Map.java:314)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1224)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2833)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at android.app.ActivityThread.performNewIntents(ActivityThread.java:2846)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2855)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at android.app.ActivityThread.access$1700(ActivityThread.java:177)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1520)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at android.os.Looper.loop(Looper.java:145)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at android.app.ActivityThread.main(ActivityThread.java:5944)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at java.lang.reflect.Method.invoke(Native Method)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at java.lang.reflect.Method.invoke(Method.java:372)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
07-16 00:19:05.663: E/AndroidRuntime(29919):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

Code: 码:

    private void gotoLocation(int id, double lat, double lng,
            String route_direct) {
         ......
        // Update the location.
        for(Iterator<MapItem> it = mapItemList.iterator(); it.hasNext();){
            //Line 132.
            MapItem item1 = it.next();

            if(item1.getId() == id){
                marker = item1.getMarker();
                marker.remove();
                //int index = mapItemList.indexOf(item1);
                it.remove();
                ll = new LatLng(lat, lng);
                MarkerOptions markerOpt = new MarkerOptions().title(
                        route_direct).position(ll);
                marker = map.addMarker(markerOpt);
                 map.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 11));
                MapItem mapItem = new MapItem(id, marker);
                mapItemList.add(mapItem);

            }

        }
}

The exception is caused by mapItemList.add(mapItem); 异常是由mapItemList.add(mapItem);引起的mapItemList.add(mapItem); . Every modification to the collection, while you are iterating over it, will cause this exception. 在迭代集合时,对集合的每次修改都会导致此异常。 From the documentation 文档中

For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception. 例如,如果线程在使用快速失败迭代器迭代集合时直接修改了集合,则迭代器将抛出此异常。

To fix you can use either use the ListIterator instead of the Iterator (to retrieve it use listIterator() ), and use iterator.add(mapItem); 要解决此问题,可以使用ListIterator而不是Iterator (要使用listIterator()检索它),并使用iterator.add(mapItem); or you can use a temporary ArrayList where you will add the items you create while you are looping, and use addAll to add the whole content to the your mapItemList after the loop ends 或者,您可以使用临时ArrayList ,在其中添加您在循环时创建的项目,并在循环结束后使用addAll将整个内容添加到mapItemList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ArrayList上的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException on ArrayList java.util.ConcurrentModificationException - ArrayList - java.util.ConcurrentModificationException - ArrayList arraylist java.util.ConcurrentModificationException从其他类初始化 - arraylist java.util.ConcurrentModificationException initialize from other class 带有自定义ArrayList的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException with custom ArrayList 多线程ArrayList上的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException on ArrayList in multi thread ArrayList处理中的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException in ArrayList processing 从Vector中移除列表,获取java.util.ConcurrentModificationException - Remove List from Vector getting java.util.ConcurrentModificationException java.util.ConcurrentModificationException不列出删除错误 - java.util.ConcurrentModificationException Not list remove error 使用INDEX删除ArrayList中的元素,从而导致java.util.ConcurrentModificationException - Removing element in ArrayList using INDEX causing java.util.ConcurrentModificationException ArrayList迭代给出异常java.util.ConcurrentModificationException - ArrayList iteration gives exception java.util.ConcurrentModificationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM