Search code examples
javaandroidandroid-custom-viewpainteraser

app crash when deleting item from array list


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 ?


Solution

  • 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.