Android 2.1 GoogleMaps ItemizedOverlay ConcurrentModificationException

后端 未结 4 1431
情话喂你
情话喂你 2020-12-18 15:51

I cannot figure out the origin of the ConcurrentModificationException. In my activity I\'m calling updateMapOverlay(). I\'m also cal

相关标签:
4条回答
  • 2020-12-18 16:27

    Generally speaking you should be safe as long as you only modify your ItemizedOverlay's backing List/Map on the UI thread.

    As Mark pointed out, AsyncTask's:

    @Override
    protected void onPostExecute(Cursor cursor) {
    
        // modify List/Map
    
        populate();
        mapView.invalidate();
    }
    

    is always executed on the UI thread, so modifications are safe here.

    0 讨论(0)
  • 2020-12-18 16:27

    I'm having the same problem. To answer your question as to "why are you removing and adding an overlay constantly?", I'm doing it because some of my overlay items might no longer exist, or new ones might appear, and existing ones may change location, depending on what's happening in the rest of my particular application. Also, I've found no way of changing the location of an OverlayItem without extending it so that I can do so.

    Besides, wouldn't changing what items are in the itemized overlay also cause a concurrent modification exception?

    0 讨论(0)
  • 2020-12-18 16:30

    Is this problem being caused because I'm invoking updateMapOverlay from inside a non-UI thread

    Yes.

    I'm also calling updateMapOverlay() inside another Thread (a TimerTask) that is invoked on regular intervals.

    Why are you removing and adding an overlay constantly? Just update the overlay and invalidate. See here for an example of updating an overlay asynchronously.

    0 讨论(0)
  • 2020-12-18 16:30

    Thank you for your response.

    However, in your code aren't you doing something similar (map.getOverlays().remove(sites); and map.getOverlays().add(sites);)?

    class OverlayTask extends AsyncTask { @Override public void onPreExecute() { if (sites!=null) { map.getOverlays().remove(sites); map.invalidate(); sites=null; } }

     @Override
     public Void doInBackground(Void... unused) {
      SystemClock.sleep(5000); // simulated work
    
     sites=new SitesOverlay();
    
     return(null);
    

    }

    @Override
    public void onPostExecute(Void unused) {
      map.getOverlays().add(sites);
      map.invalidate();
     }
    }
    
    0 讨论(0)
提交回复
热议问题