app crash when deleting item from array list

681 Views Asked by At

my app crash when i try to remove object from array list :

for (ColouredPaths p : mTouches) {
            if(erase){
                if(p!=null)
                {  mTouches.remove(p);}

            }

why this is happening and how o fix ?

1

There are 1 best solutions below

2
CJ Burkey On BEST ANSWER

If you're getting a ConcurrentException, it means you're looping through a list that you're modifying. In ArrayLists, you can't do this. Try using a Queue like this, instead of an ArrayList:

Queue<ColouredPaths> mTouches = new ConcurrentLinkedQueue<>();

You can loop through it the same way, but it shouldn't crash anymore.