안녕하세요, 저는 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
}
스택 추적을 보는 것이 도움이됩니다. 아, 그리고 줄 번호가 없으므로 게시 한 코드의 줄은 스택 추적에 표시됩니다. – Speck