0

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.

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.

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);

            }

        }
}
2
  • You're better off sticking with HashMap<Integer, Marker>. Switching to using an ArrayList will just make it harder on you, as you can't do a lookup based on the key. Commented Jul 16, 2015 at 17:07
  • 1
    @DanielNugent: Yor are right I got it to work with the HashMap. well one of the problems was, that the table id was incremented by one when the row was updated. I have not noticed that I thought it is not being incremted. Commented Jul 18, 2015 at 19:06

1 Answer 1

1

The exception is caused by 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); 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.