java.util.ConcurrentModificationException

时间:2012-03-29 10:29:46

标签: java android

//获取lat和loni throgh java.util.AbstractList中的loaddata function.java.util.ConcurrentModificationException $ SimpleListIterator.next(AbstractList.java:64)如何解决?有人说使用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();
 }

1 个答案:

答案 0 :(得分:2)

很难确切地看到异常的来源,我怀疑它实际上并不在你向我们展示的代码中。如果您提供了完整的堆栈跟踪,突出显示代码中与跟踪中的行号对应的行,我们可以跟踪它。


那导致ConcurrentModificationException的原因是什么?基本上,它们发生在修改集合时,正在使用集合的Iterator;例如像这样:

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

可能会在第二行抛出CME。避免CME的方法是在迭代它们时不修改集合......或者其他一些线程可能正在迭代它们。

有几种方法可以解决这个问题:

  • ConcurrentCxxx集合类提供不会抛出CME的迭代器。缺点是它们比普通(单线程)使用它们比类似的java.util实现类更昂贵。

  • 如果要迭代列表以删除元素,可以使用Iterator.remove()方法执行此操作; 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()
        }
    }
    

    但是,这仅适用于此特定用例。您无法添加或替换此类列表元素。