2012-06-29 73 views
1

我之后,我们调用这个methos如放大或缩小呼吁的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类如: -

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) 

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

+0

请接受/上传如果您找到您的解决方案的答案。 :) –

回答

3

您可以将您班级的所有方法Mapdatatask更改为同步方法,然后试用。

例如从

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

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

做所有的方法都是一致的,并再次检查。 :)

+0

我想把Arraylist转换成矢量如果问题是短缺,那么没有问题... – sandee

+0

@sandee是的,但为什么要冒同步时可用的风险:) –

+0

这种情况下,我现在不能成功应用你的建议.. – sandee

0

如果您调用currenttask.cancel(true);您还必须在doInBackground()方法中检查isCancelled()并停止方法执行。

而且,即使您停止前期任务,您也需要同步访问您的列表。查看其他答案!