2011-03-12 2 views
1

안녕하세요, 저는 Concurrent Modification Exception을 수정하는 데 도움이 될 수 있습니다. 목록과 스레드를 동시에 사용하려고 시도하는 것을 의미합니다. 그것을 잠그는 원인이되었지만, 나는 그것을 고치는 법을 모른다.동시 수정 예외 해결 방법?

편집이 코드는 두 번 아래 코드를 한 번만 실행하면 발생합니다.

글로벌 : 목록지도 오버레이; PointOverlay pointOverlay; getLocs에서

//Get the current overlays of the mapView and store them in the list 
mapOverlays = mapView.getOverlays(); 
//Get the image to be used as a marker 
drawable = this.getResources().getDrawable(R.drawable.guy); 
//Create the drawable and bind its centre to the bottom centre 
pointOverlay = new PointOverlay(drawable, this); 

:

에서 onCreate에서

//Used to grab location near to the phones current location and then draw 
//the location within range to the map 
public void getNearLocs(View v) 
{ 
    new Thread()//create new thread 
    { 
    public void run()//start thread 
    { 
     //Grab new location 
     loc = locManager.getLastKnownLocation(locManager.getBestProvider(locCriteria, true)); 

     //Get the lat and long 
     double lat = loc.getLatitude(); 
     double lon = loc.getLongitude(); 

     //Convert these to string to prepare for sending to server 
     String sLat = Double.toString(lat); 
     String sLon = Double.toString(lon); 

     //Add them to a name value pair 
     latLonPair.add(new BasicNameValuePair("lat", sLat)); 
     latLonPair.add(new BasicNameValuePair("lon", sLon)); 
     Log.i("getNearLocs", "Lon: " + sLon + " Lat: " + sLat);//debug 

     //http post 
     try 
     { 
      //Create a new httpClient 
      HttpClient httpclient = new DefaultHttpClient(); 
      //Create a post URL 
      HttpPost httppost = new HttpPost("http://www.nhunston.com/ProjectHeat/getLocs.php"); 
      //set the Entity of the post (information to be sent) as a new encoded URL of which the info is the nameValuePairs   
      httppost.setEntity(new UrlEncodedFormEntity(latLonPair)); 
      //Execute the post using the post created earlier and assign this to a response 
      HttpResponse response = httpclient.execute(httppost);//send data 

      //Get the response from the PHP (server) 
      InputStream in = response.getEntity().getContent(); 

      //Read in the data and store it in a JSONArray 
      JSONArray jPointsArray = new JSONArray(convertStreamToString(in)); 

      Log.i("From Server:", jPointsArray.toString()); //log the result 

      //Clear the mapView ready for redrawing 
      mapView.postInvalidate(); 

      //Loop through the JSONArray 
      for(int i = 0; i < jPointsArray.length(); i++) 
      { 
       //Get the object stored at the JSONArray position i 
       JSONObject jPointsObj = jPointsArray.getJSONObject(i); 

       //Extract the values out of the objects by using their names 
       //Cast to int 
       //Then* 1e6 to convert to micro-degrees 
       GeoPoint point = new GeoPoint((int)(jPointsObj.getDouble("lat") *1e6), 
               (int)(jPointsObj.getDouble("lon") *1e6)); 
       //Log for debugging 
       Log.i("From Server:", String.valueOf((int) (jPointsObj.getDouble("lat") * 1e6))); //log the result 
       Log.i("From Server:", String.valueOf((int) (jPointsObj.getDouble("lon") * 1e6))); //log the result 

       //Create a new overlayItem at the above geoPosition (text optional) 
       OverlayItem overlayitem = new OverlayItem(point, "Test", "Test"); 

       //Add the item to the overlay 
       pointOverlay.addOverlay(overlayitem); 
       //Add the overlay to the mapView 
       mapOverlays.add(pointOverlay); 
       //mapView.refreshDrawableState(); 
      } 
     } 
     catch(Exception e) 
     { 
      Log.e("getNearLocs", e.toString()); 
     } 
    } 
}.start();//start thread 
} 
+2

스택 추적을 보는 것이 도움이됩니다. 아, 그리고 줄 번호가 없으므로 게시 한 코드의 줄은 스택 추적에 표시됩니다. – Speck

답변

4

다른 스레드가 목록을 읽는 동안 오버레이 항목의 목록을 수정하는 것이 문제입니다.

문제가 백그라운드 작업을 수행하는 방식과 관련이 있다고 생각됩니다. UI (메인 스레드)에서만 UI를 수정할 수 있습니다. 스레드의 맵에 오버레이 항목을 추가하면 안됩니다. 백그라운드 작업을 올바르게 수행하고 UI를 업데이트하는 방법을 보려면 AsyncTask을 확인하십시오. Android 개발자 사이트에서 Threading에 대한 기사를 읽는 것도 도움이됩니다.

4

문제는 입니다 가능성이 mapOverlays.add로 전화(). 이것은 다른 스레드 또는 코드 조각이 목록을 반복하는 것과 동시에 발생하는 것입니다. 동시 수정 예외는 한 스레드가 컬렉션 (일반적으로 반복자 사용)을 반복하고 다른 스레드가 구조적으로 컬렉션을 변경하려고 시도하면 throw됩니다.

두 개의 다른 스레드에서 동시에 mapOverlays에 액세스하고 목록에서 동기화 할 수있는 장소를 찾는 것이 좋습니다.

+0

아래 표의 내용이 확실하지 않은 경우 –

관련 문제