Say I have an ArrayList of “blob” objects, which I cycle through and draw then onto the canvas.
for (blob b:myBlobList)
{
canvas.drawCircle(b.X, b.Y, b.Size, paint);
}
Also I have an onTouchListener that adds a new blob object whenever the surface is touched.
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN)
{
myBlobList.add(new blob());
myBlobList.get(myBlobList.size()-1).X = event.getX();
myBlobList.get(myBlobList.size()-1).Y = event.getY();;
}
Yet together they cause a ConcurrentModificationException error. What would be your suggestion, how should I fix that?
Simply adding
try{ ... } catch (ConcurrentModificationException e) {}
Makes the screen/canvas flicker when this exception happens and it goes happen a lot. 🙂
Thanks!
It seems that you add a new blob to your list while iterating over that list. One simple workaround would be to make a copy of the list before the loop. If you add items while iterating they will not be taken into account.