android - 异步任务抛出并发修改异常?

标签 android google-maps

我在放大或缩小后调用 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)

我该如何处理这种情况,请尽快帮助我。

最佳答案

你可以把你的类Mapdatatask的所有方法都改成synchronized方法,然后试试。

例如来自

protected Integer doInBackground(Void... params) {
....    
}

protected synchronized Integer doInBackground(Void... params) {
....    
}

对所有方法执行相同的操作并再次检查。 :)

关于android - 异步任务抛出并发修改异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11257003/

相关文章:

android - 如何添加两个android :name attributes

java - 根据不同布局中的条目检查 editText 条目

android - OpenGL ES 2 球体渲染

javascript - Google Maps API 自动完成 - 如何仅输出街道名称?

android - PlaceAutocompleteFragment 在点击时消失

javascript - 如何在 Google Map Api 3 的 InfoBox 窗口中隐藏关闭(x)按钮?

java - "wrong first argument type. Found: ' android.widget.Compoundbutton.oncheckedchangelistener ', required: ' android.content.context '"

android - 你可以在RelativeLayout中居中一个按钮吗?

android - Maps api v2 android 我的位置不适用于 2.3.3

javascript - javascript中可以有多个同名对象吗?