2012-03-09 89 views
0

我嘗試刷新並重新繪製地圖每15秒,但我得到ConcurrentModificationException的有一個ArrayList請嘗試刷新並重新繪製地圖,堅持例外

在的onCreate

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.map); 
    Log.i("MapARS", "Start MapARS Activity"); 

    Bundle extras = getIntent().getExtras(); 
    requestSeq = extras.getString("requestSeq"); 
    requestLat = extras.getDouble("requestLat"); 
    requestLng = extras.getDouble("requestLng"); 

    mapView = (MapView) findViewById(R.id.mapview); 
    mapView.setBuiltInZoomControls(true); 
    myMapController = mapView.getController(); 

    GeoPoint geoPointRequester = new GeoPoint((int)(requestLat*1E6),(int)(requestLng*1E6));//set Requester GeoPoint 
    myMapController.animateTo(geoPointRequester); 

    mapListOverlays = mapView.getOverlays(); 
    drawableRequester = this.getResources().getDrawable(R.drawable.pin02); 
    drawableVolunteer = this.getResources().getDrawable(R.drawable.pin01); 
    itemizedoverlayRequester = new ARSItemizedOverlay(drawableRequester, this); 
    itemizedoverlayVolunteer = new ARSItemizedOverlay(drawableVolunteer, this); 

    OverlayItem overlayRequester = new OverlayItem(geoPointRequester, requestSeq, ""); 
    itemizedoverlayRequester.addOverlay(overlayRequester); 
    mapListOverlays.add(itemizedoverlayRequester); 

    volunteerList = new ArrayList<Volunteer>(); 

    updateThread = new UpdateThread(); 
    updateThread.start(); 
} 

這是我的線程類

public class UpdateThread extends Thread { 
    public void run(){ 

     while(true) { 
      volunteerList.clear(); 
      volunteerList = getRequestMemberRDetail(); 
      drawVolunteer(); 
      try { 
       sleep((long) 15000.0); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    private ArrayList<Volunteer> getRequestMemberRDetail() { 
     try { 
      result = RequesterService.getRequestMemberR(requestSeq); 
      for (int i=0;i<result.getPropertyCount();i++) { 
       SoapObject temp = (SoapObject) result.getProperty(i); 
       Volunteer volunteerDetail = new Volunteer(); 
       volunteerDetail.member_Seq = temp.getProperty("Member_Seq").toString();     
       volunteerDetail.member_FirstName = temp.getProperty("Member_FirstName").toString();     
       volunteerDetail.member_LastName = temp.getProperty("Member_LastName").toString(); 
       volunteerDetail.member_TelNo = temp.getProperty("Member_TelNo").toString(); 
       volunteerDetail.member_Lat = Double.parseDouble(temp.getProperty("Member_Lat").toString());     
       volunteerDetail.member_Lng = Double.parseDouble(temp.getProperty("Member_Lng").toString());     

       volunteerList.add(volunteerDetail); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return volunteerList; 
    } 

    private void drawVolunteer() { 
     for(int i=0;i<volunteerList.size();i++) { 
      GeoPoint tempGeoPoint = new GeoPoint((int)(volunteerList.get(i).member_Lat*1E6) 
               ,(int)(volunteerList.get(i).member_Lng*1E6)); 
      OverlayItem tempOverlay = new OverlayItem(tempGeoPoint, volunteerList.get(i).member_FirstName, ""); 
      itemizedoverlayVolunteer.addOverlay(tempOverlay); 
      mapListOverlays.add(itemizedoverlayVolunteer); 
      mapView.invalidate(); 
     } 
    }*/ 

我在谷歌找到ConcurrentModificationException,但我仍然不明白爲什麼代碼不工作

任何建議,將不勝感激。 謝謝:P

回答

0

嘗試從onCreate()方法去除

volunteerList = new ArrayList<Volunteer>(); 

,並

public class UpdateThread extends Thread { 
public void run(){ 

    while(true) { 
     List<Volunteer> volunteerList = getRequestMemberRDetail(); 
     drawVolunteer(volunteerList); 
     try { 
      sleep((long) 15000.0); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

private ArrayList<Volunteer> getRequestMemberRDetail() { 
    List<Volunteer> volunteerList = new ArrayList<Volunteer>(); 
    try { 
     result = RequesterService.getRequestMemberR(requestSeq); 
     for (int i=0;i<result.getPropertyCount();i++) { 
      SoapObject temp = (SoapObject) result.getProperty(i); 
      Volunteer volunteerDetail = new Volunteer(); 
      volunteerDetail.member_Seq = temp.getProperty("Member_Seq").toString();     
      volunteerDetail.member_FirstName = temp.getProperty("Member_FirstName").toString();     
      volunteerDetail.member_LastName = temp.getProperty("Member_LastName").toString(); 
      volunteerDetail.member_TelNo = temp.getProperty("Member_TelNo").toString(); 
      volunteerDetail.member_Lat = Double.parseDouble(temp.getProperty("Member_Lat").toString());     
      volunteerDetail.member_Lng = Double.parseDouble(temp.getProperty("Member_Lng").toString());     

      volunteerList.add(volunteerDetail); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return volunteerList; 
} 

private void drawVolunteer(List<Volunteer> volunteerList) { 
    for(Volunteer volunteer: volunteerList) { 
     GeoPoint tempGeoPoint = new GeoPoint((int)(volunteer.member_Lat*1E6) 
              ,(int)(volunteer.member_Lng*1E6)); 
     OverlayItem tempOverlay = new OverlayItem(tempGeoPoint, volunteerList.get(i).member_FirstName, ""); 
     itemizedoverlayVolunteer.addOverlay(tempOverlay); 
     mapListOverlays.add(itemizedoverlayVolunteer); 
     mapView.invalidate(); 
    } 
}*/ 
+0

如何知道在drawVolunteer方法循環的指標? 對不起,我的啞巴:'( – Vazedias 2012-03-09 20:11:12

+0

ohhh這是我的愚蠢問題!!現在我明白索引 – Vazedias 2012-03-09 20:14:22

+0

如果你真的需要索引 - 你可以使用for(int i = ...),但在你的情況下,它是沒有必要 – 2012-03-09 20:18:54