-1

//get the lat and loni throgh the loaddata function.java.util.ConcurrentModificationException at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:64) How to resolve it?Somebody said use Iterator.

public void loaddata()
 {
     try
     {

            lat = new String[vechno.length];
            lon=new String[vechno.length];
            for(int i=0;i<vechno.length;i++)
            {

                if(Integer.parseInt(vechspeed[i].trim())==0||vechspeed[i].trim()=="")
                {
                    latiloni=call2(vechno[i].toString());
                    lat[i]=latiloni[0];
                    lon[i]=latiloni[1];
                    GeoPoint point1 = new GeoPoint((int)(Double.parseDouble(lat[i])*1E6),(int)(Double.parseDouble(lon[i])*1E6));
                    OverlayItem overlayItem = new OverlayItem(point1,"Vehicle No : "+vechno[i]+"(Idle)", 
                            "Located           : "+vechloc[i]+"\n"+"Speed(kmph) : "+vechspeed[i]+"\n"+"Address           : "+vechadd[i]);
                    itemizedOverlay0.addOverlay(overlayItem);
                    final MapController mc = mapView.getController();
                    mc.animateTo(point1);
                    mc.setZoom(6);
                    mapOverlays.add(itemizedOverlay0);


                }
                else
                if(Integer.parseInt(vechspeed[i].trim())>=1&&Integer.parseInt(vechspeed[i].trim())<=20)
                {
                    latiloni=call2(vechno[i].toString());
                    lat[i]=latiloni[0];
                    lon[i]=latiloni[1];
                    GeoPoint point2 = new GeoPoint((int)(Double.parseDouble(lat[i])*1E6),(int)(Double.parseDouble(lon[i])*1E6));
                    OverlayItem overlayItem = new OverlayItem(point2,"Vehicle No : "+vechno[i]+"(Running)", 
                            "Located           : "+vechloc[i]+"\n"+"Speed(kmph) : "+vechspeed[i]+"\n"+"Address           : "+vechadd[i]);
                    itemizedOverlay1.addOverlay(overlayItem);
                    final MapController mc = mapView.getController();
                    mc.animateTo(point2);
                    mc.setZoom(6);
                    mapOverlays.add(itemizedOverlay1);

                }
                else
                if(Integer.parseInt(vechspeed[i].trim())>=21&&Integer.parseInt(vechspeed[i].trim())<=40)
                {
                        latiloni=call2(vechno[i].toString());
                        lat[i]=latiloni[0];
                        lon[i]=latiloni[1];
                        GeoPoint point3 = new GeoPoint((int)(Double.parseDouble(lat[i])*1E6),(int)(Double.parseDouble(lon[i])*1E6));
                        OverlayItem overlayItem = new OverlayItem(point3,"Vehicle No : "+vechno[i]+"(Running)", 
                                "Located           : "+vechloc[i]+"\n"+"Speed(kmph) : "+vechspeed[i]+"\n"+"Address           : "+vechadd[i]);
                        itemizedOverlay2.addOverlay(overlayItem);
                        final MapController mc = mapView.getController();
                        mc.animateTo(point3);
                        mc.setZoom(6);
                        mapOverlays.add(itemizedOverlay2);

                }
                else
                if(Integer.parseInt(vechspeed[i].trim())>=41&&Integer.parseInt(vechspeed[i].trim())<=60)
                {
                        latiloni=call2(vechno[i].toString());
                        lat[i]=latiloni[0];
                        lon[i]=latiloni[1];
                        GeoPoint point4 = new GeoPoint((int)(Double.parseDouble(lat[i])*1E6),(int)(Double.parseDouble(lon[i])*1E6));
                        OverlayItem overlayItem = new OverlayItem(point4,"Vehicle No : "+vechno[i]+"(Running)", 
                                "Located           : "+vechloc[i]+"\n"+"Speed(kmph) : "+vechspeed[i]+"\n"+"Address           : "+vechadd[i]);
                        itemizedOverlay3.addOverlay(overlayItem);
                        final MapController mc = mapView.getController();
                        mc.animateTo(point4);
                        mc.setZoom(6);
                        mapOverlays.add(itemizedOverlay3);

                }
                else
                if(Integer.parseInt(vechspeed[i].trim())>=61&&Integer.parseInt(vechspeed[i].trim())<=80)
                {
                        latiloni=call2(vechno[i].toString());
                        lat[i]=latiloni[0];
                        lon[i]=latiloni[1];
                        GeoPoint point5 = new GeoPoint((int)(Double.parseDouble(lat[i])*1E6),(int)(Double.parseDouble(lon[i])*1E6));
                        OverlayItem overlayItem = new OverlayItem(point5,"Vehicle No : "+vechno[i]+"(Running)", 
                                "Located           : "+vechloc[i]+"\n"+"Speed(kmph) : "+vechspeed[i]+"\n"+"Address           : "+vechadd[i]);
                        itemizedOverlay4.addOverlay(overlayItem);
                        final MapController mc = mapView.getController();
                        mc.animateTo(point5);
                        mc.setZoom(6);
                        mapOverlays.add(itemizedOverlay4);

                }
                else
                if(Integer.parseInt(vechspeed[i].trim())>=81&&Integer.parseInt(vechspeed[i].trim())<=120)
                {

                        latiloni=call2(vechno[i].toString());
                        lat[i]=latiloni[0];
                        lon[i]=latiloni[1];
                        GeoPoint point6 = new GeoPoint((int)(Double.parseDouble(lat[i])*1E6),(int)(Double.parseDouble(lon[i])*1E6));
                        OverlayItem overlayItem6 = new OverlayItem(point6,"Vehicle No : "+vechno[i]+"(Running)", 
                                "Located           : "+vechloc[i]+"\n"+"Speed(kmph) : "+vechspeed[i]+"\n"+"Address           : "+vechadd[i]);
                        itemizedOverlay5.addOverlay(overlayItem6);
                        final MapController mc = mapView.getController();
                        mc.animateTo(point6);
                        mc.setZoom(6);
                        mapOverlays.add(itemizedOverlay5);

                }
                else
                if(Integer.parseInt(vechspeed[i].trim())>=120&&Integer.parseInt(vechspeed[i].trim())<=150)
                {
                        latiloni=call2(vechno[i].toString());
                        lat[i]=latiloni[0];
                        lon[i]=latiloni[1];
                        GeoPoint point7 = new GeoPoint((int)(Double.parseDouble(lat[i])*1E6),(int)(Double.parseDouble(lon[i])*1E6));
                        OverlayItem overlayItem = new OverlayItem(point7,"Vehicle No : "+vechno[i]+"(Running)", 
                                "Located           : "+vechloc[i]+"\n"+"Speed(kmph) : "+vechspeed[i]+"\n"+"Address           : "+vechadd[i]);
                        itemizedOverlay6.addOverlay(overlayItem);
                        final MapController mc = mapView.getController();
                        mc.animateTo(point7);
                        mc.setZoom(6);
                        mapOverlays.add(itemizedOverlay6);

                }   

            }
            mapView.invalidate();
     }
     catch(Exception e)
     {

     }

 }
 protected void dosomthing()
 {
        progressdialog1=ProgressDialog.show(Mapping.this, "", "Loading.Please wait...");
        Runnable threadTask = new Runnable() {

            @Override
            public void run() {

                while(true){
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    loaddata();
                    messageHandler.sendEmptyMessage(0);

                }
            }
        };
        (new Thread(threadTask)).start();
 }
user1300479
  • 11
  • 1
  • 2

1 Answers1

2

It is difficult to see exactly where the exception is coming from, and I suspect that it is not actually in the code you are showing us. If you provided a full stack trace, highlighted the lines in your code that correspond to the line numbers in the trace, we could maybe track it down.


So what causes ConcurrentModificationExceptions? Well basically, they happen when a collection is modified while an Iterator for the collection is being used; e.g. like this:

    List<String> l = ...
    for (String s : l) {
        if (s.equals("42")) {
            l.add("43");
        }
    }

is likely to throw a CME on the 2nd line if it ever finds a "42". The way to avoid CMEs is to not modify collections while iterating them ... or while some other thread might be iterating them.

There are a couple of ways to get around this:

  • The ConcurrentCxxx collection classes provide iterators that won't throw a CME. The downside is that they are a more expensive for normal (single-threaded) use than the analogous java.util implementation classes.

  • If you are iterating the list in order to remove elements, you can do this using the Iterator.remove() method; e.g.

    List<String> l = ...
    Iterator<String> it = l.iterator();
    while (l.hasNext()) {
        String s = it.next();
        if (s.equals("42")) {
            l.remove();   // removes the string returned by last call to next()
        }
    }
    

    However, this only works for this particular use-case. You can't add or replace list elements like this.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 03-29 15:48:59.271: E/AndroidRuntime(316): Uncaught handler: thread main exiting due to uncaught exception 03-29 15:48:59.330: E/AndroidRuntime(316): java.util.ConcurrentModificationException 03-29 15:48:59.330: E/AndroidRuntime(316): at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:64) 03-29 15:48:59.330: E/AndroidRuntime(316): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:44) – user1300479 Mar 29 '12 at 11:12
  • There's no call to `draw` in your code. It looks like your code must be adding overlays at the same time that something else (an event listener thread?) is iterating over them in order to draw them. You are probably not supposed to do this ... but I'm not an Android developer. – Stephen C Mar 29 '12 at 11:32