我在放大或缩小谷歌地图后调用AsyncTask,我们将此方法称为: -
public void doListing(String URL) {
mapView.getOverlays().clear();
ListURL = URL;
System.out.println("Inside dolist methods----"+URL);
Eyelidmessage("Loading...");
if(currenttask == null)
{
currenttask = new Mapdatatask();
currenttask.execute();
}
else if(currenttask != null && currenttask.getStatus()== AsyncTask.Status.FINISHED)
{
currenttask = null ;
currenttask = new Mapdatatask();
currenttask.execute();
}
else if(currenttask != null && currenttask.getStatus()== AsyncTask.Status.RUNNING)
{
Log.e("currenttask",""+currenttask.getStatus());
currenttask.cancel(true);
currenttask = null ;
currenttask = new Mapdatatask();
currenttask.execute();
}
else if(currenttask != null && currenttask.getStatus()== AsyncTask.Status.PENDING)
{
Log.e("currenttask",""+currenttask.getStatus());
currenttask.cancel(true);
currenttask = null ;
currenttask = new Mapdatatask();
currenttask.execute();
}
}
Mapdatatask()是一个AsyncTask类As: -
private class Mapdatatask extends AsyncTask<Void, Void, Integer>
{
@Override
protected void onPreExecute()
{
arrayList= new ArrayList<Applicationdataset>();
}
@Override
protected Integer doInBackground(Void... params) {
System.out.println("----ListURL IN doin Background-----"+ListURL);
try
{
StringBuilder sb=new StringBuilder();
URL sourceUrl=new URL(ListURL);
InputStream inputStream=sourceUrl.openStream();
//add if necessary the PushBack measure to safeguard the intended purpose here if really happening
//that you can actually ascertain it by saving a local copy for inspection
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line=br.readLine())!=null) {
sb.append(line+"\n");
}
br.close();
SAXParserFactory spff=SAXParserFactory.newInstance();
spff.setNamespaceAware(true);
SAXParser spp=spff.newSAXParser();
XMLReader xr=spp.getXMLReader();
myHandler=new ApplicationHandler();
xr.setContentHandler(myHandler);
System.out.println("resultttt-----"+sb.toString());
xr.parse(new InputSource(new StringReader(sb.toString())));
//xr.parse(new InputSource(sourceUrl.openStream()));
valueoftotal = ApplicationHandler.value;
arrayList = myHandler.getParsedData();
System.out.println("total value------"+valueoftotal);
}
catch(IOException e) {
e.printStackTrace();
} catch (SAXException e)
{
arrayList= new ArrayList<Applicationdataset>();
try
{
StringBuilder sb=new StringBuilder();
URL sourceUrl=new URL(ListURL);
InputStream inputStream=sourceUrl.openStream();
//add if necessary the PushBack measure to safeguard the intended purpose here if really happening
//that you can actually ascertain it by saving a local copy for inspection
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line=br.readLine())!=null) {
sb.append(line+"\n");
}
br.close();
inputStream.close();
SAXParserFactory spff=SAXParserFactory.newInstance();
spff.setNamespaceAware(true);
SAXParser spp=spff.newSAXParser();
XMLReader xr=spp.getXMLReader();
myHandler=new ApplicationHandler();
xr.setContentHandler(myHandler);
xr.parse(new InputSource(new StringReader(sb.toString())));
//xr.parse(new InputSource(sourceUrl.openStream()));
valueoftotal = ApplicationHandler.value;
arrayList = myHandler.getParsedData();
//arrayList = myHandler.getParsedData();
}
catch(IOException e1) {
arrayList.clear();
e1.printStackTrace();
Log.e("parse exception0"," dont know why");
Log.e("XML Error ",e.toString());
} catch (SAXException e1)
{
arrayList.clear();
Log.e("parse exception1"," dont know why");
e1.printStackTrace();
} catch (ParserConfigurationException e1)
{
arrayList.clear();
Log.e("parse exception2"," dont know why");
e1.printStackTrace();
} catch (ParseException e1) {
Log.e("parse exception3"," dont know why");
e1.printStackTrace();
}
e.printStackTrace();
} catch (ParserConfigurationException e) {
arrayList.clear();
Log.e("parse exception2"," dont know why");
e.printStackTrace();
} catch (ParseException e) {
arrayList.clear();
Log.e("parse exception3"," dont know why");
e.printStackTrace();
}
return 1;
}
@Override
protected void onPostExecute(Integer result)
{
if(valueoftotal!=null && !valueoftotal.equalsIgnoreCase(""))
{
if(Integer.parseInt(valueoftotal) > 0 && Integer.parseInt(valueoftotal) <= 100)
{
OverlayItem oi =null;
if(myHandler.getParsedData()!=null)
{
myvectorlist = new Vector<ArrayList<Applicationdataset>>();
System.out.println(arrayList.size()+"++++++++++");
Map<Key, ArrayList<Applicationdataset>> mlistVector = groupTheList(arrayList);
myvectorlist = putIntoVector(mlistVector);
System.out.println("oh nioce----"+myvectorlist.size());
}
linearbottom2.setClickable(true);
mapOverlays.clear();
//overlay.removeAll();
overlay = new GoogleMapViewOverlay(drawable,mapView,activity);
lineartabbar.setBackgroundResource(R.drawable.mapbar);
if(myvectorlist != null && myvectorlist.size() > 1)
{
Applicationdataset data = null;
try {
for(int i = 0; i < myvectorlist.size(); i++)
{
data = myvectorlist.get(i).get(0);
String latvalue = data.getLatitude().toString();
String lonvalue = data.getLongitude().toString();
System.out.println("+++1 hhh+++");
GeoPoint point = null;
try
{
point = new GeoPoint((int)(Double.parseDouble(latvalue)*1E6),(int)(Double.parseDouble(lonvalue)*1E6));
}
catch (NumberFormatException e)
{
e.printStackTrace();
}
if(point != null)
{
System.out.println("Above size of arraylist---=="+overlay.size()+"======index of array"+i);
if(overlay.size() == i)
{
oi = overlay.createItem(i);
}
if(i>overlay.size())
{
Log.e("Error-------------", "Overlay size= "+overlay.size() + " index = "+i);
Log.e("Error-------------", "");
continue;
}
System.out.println("size of arraylist---=="+overlay.size()+"======index of array"+i);
if(overlay!=null && oi!=null)
{
overlay.addItem(oi);
if(myvectorlist.size() == 1)
{
mc.animateTo(point);
mc.setCenter(point);
mc.setZoom(16);
}
}
}
}//end for loop
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("+++size of overlay++++ "+overlay.size());
mapOverlays.add(overlay);
mapView.invalidate();
}
textViewpopup.setText(valueoftotal+" listings found.");
linearlayoutpopup.setVisibility(View.VISIBLE);
RunAnimationslisting();
}
else if(Integer.parseInt(valueoftotal) > 100)
{
Eyelidmessage(valueoftotal+" listings found. \n Zoom-in, press the locate button below or select the refine button above to display fewer properties. ");
lineartabbar.setBackgroundResource(R.drawable.mapwithoutlist);
linearbottom2.setClickable(false);
}
else if(Integer.parseInt(valueoftotal) == 0)
{
GoneAnimations();
lineartabbar.setBackgroundResource(R.drawable.mapwithoutlist);
linearbottom2.setClickable(false);
customizeDialog = new CustomizeDialog(activity);
customizeDialog.setTitle("Sorry");
customizeDialog.setMessage("No Listing Found");
customizeDialog.show();
}
}
super.onPostExecute(result);
}
当我放大和缩小时,谷歌地图会抛出此异常异常堆栈跟踪如下: -
06-29 10:27:26.100: E/AndroidRuntime(9683): FATAL EXCEPTION: main
06-29 10:27:26.100: E/AndroidRuntime(9683): java.util.ConcurrentModificationException
06-29 10:27:26.100: E/AndroidRuntime(9683): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
06-29 10:27:26.100: E/AndroidRuntime(9683): at com.rentfaster.home.GoogleMapView.groupTheList(GoogleMapView.java:2305)
06-29 10:27:26.100: E/AndroidRuntime(9683): at com.rentfaster.home.GoogleMapView.access$1(GoogleMapView.java:2303)
06-29 10:27:26.100: E/AndroidRuntime(9683): at com.rentfaster.home.GoogleMapView$Mapdatatask.onPostExecute(GoogleMapView.java:1416)
06-29 10:27:26.100: E/AndroidRuntime(9683): at com.rentfaster.home.GoogleMapView$Mapdatatask.onPostExecute(GoogleMapView.java:1)
06-29 10:27:26.100: E/AndroidRuntime(9683): at android.os.AsyncTask.finish(AsyncTask.java:590)
06-29 10:27:26.100: E/AndroidRuntime(9683): at android.os.AsyncTask.access$600(AsyncTask.java:149)
06-29 10:27:26.100: E/AndroidRuntime(9683): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
06-29 10:27:26.100: E/AndroidRuntime(9683): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 10:27:26.100: E/AndroidRuntime(9683): at android.os.Looper.loop(Looper.java:132)
06-29 10:27:26.100: E/AndroidRuntime(9683): at android.app.ActivityThread.main(ActivityThread.java:4126)
06-29 10:27:26.100: E/AndroidRuntime(9683): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 10:27:26.100: E/AndroidRuntime(9683): at java.lang.reflect.Method.invoke(Method.java:491)
06-29 10:27:26.100: E/AndroidRuntime(9683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
06-29 10:27:26.100: E/AndroidRuntime(9683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
06-29 10:27:26.100: E/AndroidRuntime(9683): at dalvik.system.NativeStart.main(Native Method)
我如何处理这种情况请任何人尽快帮助我。
答案 0 :(得分:3)
您可以将班级 Mapdatatask
的所有方法更改为同步方法,然后尝试。
例如来自
protected Integer doInBackground(Void... params) {
....
}
要
protected synchronized Integer doInBackground(Void... params) {
....
}
对所有方法执行相同操作并再次检查。 :)
答案 1 :(得分:0)
如果你调用currenttask.cancel(true);你还必须在doInBackground()方法中检查isCancelled()并停止方法执行。
即使您停止了前任任务,也需要同步访问您的列表。见其他答案!