Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:57:13+00:00 2026-06-02T23:57:13+00:00

First of all, i don’t know where, i don’t know why, i get ConcurrentModificationException.

  • 0

First of all, i don’t know where, i don’t know why, i get ConcurrentModificationException. My Logcat is out of order (or just i can’t use it) but never shows any information about exceptions ( i read lots of article about it, and nothing helped, maybe can’t debug my phone correctly )

Secondly sorry about my confused english, i try to formulate as clearly as i can, codes could help, please help me

So, The problem is the next:

i use Mapview and 5 custom CustomItemizedOverlay (Source no. 1) on it.
From MapView i start some (1, 2, max 5) threads to webservice (Source no. 2) and after i get results back (it’s List) i draw them into mapoverlay (source no. 3)

so MapView ( implements 5 ResponseHandlerInterfaces ) sends requests to webservice through myActions (extends Thread) and when actions gets responses, they call responseHandler.reportResponseList(List list) methods. (MapView get back the control right here)

and all of it causes ConcurrentModificationException sometimes
(rarely ArrayIndexOutOfBoundsException)

i have got Options Activity to set required lists and i also have got Refresh button, to get lists. let me lead you through one example.

I just opened MapView, it’s empty. I need only 1 kind of objects. I tap refresh, after network communication, i get markers on my mapview. cool, it’s working. Now i’m going to Options, and i set more objects to request. Use Refresh again at mapview, and sometimes i get all kinds of objects, sometimes i get ConcurrentModificationException.

Source No. 1

public class CustomItemizedOverlay<T> extends ItemizedOverlay<T>{

private Context mContext;
private Object lock = new Object();
private CopyOnWriteArrayList<T> overlays = new CopyOnWriteArrayList<T>();

public CustomItemizedOverlay(Drawable marker, Context context) {
    super(boundCenterBottom(marker));
    this.mContext = context;
    populate();
}
@Override
protected boolean onTap(int index){
 // doesn't matter
}
public void clear(){
    synchronized (lock) {
        overlays.clear();
    }
}
public void addOverlay(T overlay){
    synchronized (lock) {
        overlays.add(overlay);
        setLastFocusedIndex(-1);
        populate();
    }
}
public void removeOverlay(int selected){
    synchronized (lock) {
        overlays.remove(selected);
        populate();
        setLastFocusedIndex(-1);
    }
}
@Override
protected T createItem(int i) {
    synchronized (lock) {
        return overlays.get(i);         
    }
}
@Override
public int size() {
    synchronized (lock) {
        return overlays.size();
    }
}
public void setLock(Object o){
        this.lock = o;
    }
}

Source No. 2

MapView:

public class MyMap extends MapActivity implements LocationListener, RestResPonseHandler { // there are 5 type of responsehandlers, one for each overlay 

private MapView mapView;
private MyLocationOverlay myLocationOverlay;

private Object lock = new Object();

private CustomItemizedOverlay<CustomOverlayItem<MyObject1>> my1Overlay;
private CustomItemizedOverlay<CustomOverlayItem<MyObject2>> my2Overlay;
private CustomItemizedOverlay<CustomOverlayItem<MyObject3>> my3Overlay;
private CustomItemizedOverlay<CustomOverlayItem<MyObject4>> my4Overlay;
private CustomItemizedOverlay<CustomOverlayItem<MyObject5>> my5Overlay;

public void getObject1List(){ // there are 5 getList methods
    new RestAction(this).start(); // 'this' is the object which implements required RestResponseHandler interface. in every case it will be 'this'. MyMap implements all kind of required RestResponseHandler interfaces
    }

Source No. 3 (non main thread) // This is pattern for each ‘CustomItemizedOverlay filling method’. After actions reports results (list of objects), mapview fills actual overlay with OverlayItems

@Override
public void reportResponseList(List<MyObject1> objects) {
    if (my1Overlay == null){
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable marker = this.getResources().getDrawable(R.drawable.icon);
        my1Overlay = new CustomItemizedOverlay<CustomOverlayItem<MyObject1>>(marker, this);
        my1Overlay.setLock(lock); // MyMap has lock object, look at source 2 (also CustomItemizedOverlay (source 1) )
        mapOverlays.add(my1Overlay);
    } else {
        my1Overlay.clear(); 
    }   
    synchronized (lock) {
        for(int i=0;i<objects.size();++i){
            MyObject1 object = objects.get(i);
            CustomOverlayItem<MyObject1> item = new CustomOverlayItem<CustomBuilding>(object.getPositionId(), object);
            my1Overlay.addOverlay(item);
        }
    refreshView();
    }
}

Where refreshView posts runnable to main thread to update mapView.

public void refreshView(){
    new Thread(new Runnable(){
        @Override
        public void run(){
            mapView.post(new Runnable(){
                @Override
                public void run(){
                    mapView.invalidate();
                }
            }); 
        }
    }).start();
}

The Solution:
After CommonsWare’s answer, i modified my source to :

@Override
public void reportResponseList(final List<MyObject1> objects) {     
    if (my1Overlay == null){
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable marker = this.getResources().getDrawable(R.drawable.icon);
        my1Overlay = new CustomItemizedOverlay<CustomOverlayItem<MyObject1>>(marker, this);
        my1Overlay.setLock(lock);
        mapOverlays.add(my1Overlay);
    } else {
        runOnUiThread(new Runnable(){
            @Override
            public void run(){
                my1Overlay.clear();
            }
        }); 
    }
    runOnUiThread(new Runnable(){
        @Override
        public void run(){
            for(int i=0;i<objects.size();++i){
                MyObject1 object = objects.get(i);
                CustomOverlayItem<MyObject1> item = new CustomOverlayItem<MyObject1>(object.getPositionId(), object);
                my1Overlay.addOverlay(item);
            }
            refreshView();  
        }
    });
}

and now at this moment it seems to work. i don’t know how pretty is it, but seems to work. (maybe mapOverlays.add() method should be on main thread too) Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:57:14+00:00Added an answer on June 2, 2026 at 11:57 pm

    If my1Overlay is already part of the MapView by the time reportResponseList() is called, you should not be modifying it on a background thread. MapView will be using that Overlay. Instead, create a new Overlay in the background thread, the swap overlays (remove the old, add the new) on the main application thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I don't want to use any framework but I am looking
First of all, I know about the command line parameter, but I don't want
First of all I don't know if this is the right approach. I want
First of all, I don't want to use a join because that will make
First of all I don't know if it this question belongs here if not
First of all, I really don't know the correct direction on where to search
First of all I'm using netbeans as my IDE and I don't know if
First of all, I don't have multiplication, division operations so i could use shifting/adding,
Hi All First of all I don't know Javascript, I got this script from
First of all I am an autodidact so I don't have great know how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.