title: java.util.ConcurrentModificationException in Android animation
body: <p>There is something I miss with the notion of Synchronizing code in Android.</p>
  
  <p><strong>Scenario</strong></p>
  
  <p>There are always 3 items drawn on the screen. Each image is stored in a ArrayList (lstGraphics). For this purpose I use a SurfaceView. Once the user taps on a image, the image get's market to be removed and a new one will be added.</p>
  
  <p>Code samples:</p>
  
  <p>AnimationHideThread</p>
  
  <pre><code>...
      @Override
          public void run() {
              Canvas c;
              while (run) {
                  c = null;
                  try {
                      c = panel.getHolder().lockCanvas(null);
                        synchronized (panel.getHolder()) {
  
                          panel.updatePhysics();
                          panel.manageAnimations();
                          panel.onDraw(c);
  
                      }
                  } finally {
                      if (c != null) {
                          panel.getHolder().unlockCanvasAndPost(c);
                      }
                  }
              }
          }    
  ...
  </code></pre>
  
  <p>So as you can seem first I updatePhysics(). This means I calculate direction where each image will move to. In here I will also remove clicked images from my list. After that I check if I need to add a new Item in my list in manageAnimations() and then the final step draw the whole thing.</p>
  
  <pre><code>public class Panel extends SurfaceView implements SurfaceHolder.Callback {
  ....
   public void manageAnimations()
      {
            synchronized (this.getHolder()) {
              ...
          while (lstGraphics.size()&lt;3) {
                  lstGraphics.add(createRandomGraphic());
                  }
          }
            }
      }
  
   @Override
      public boolean onTouchEvent(MotionEvent event) {
           synchronized (getHolder()) {
              if (event.getAction() == MotionEvent.ACTION_DOWN) {
                   //... check if a image has been clicked and then set its property
                          graphic.setTouched(true);
  
                   }
              }
  
              return true;
           }
      }
  
   public void updatePhysics() {
         synchronized (getHolder()) {
  
       for (Graphic graphic : lstGraphics) {
             //.... Do some checks
       if (graphic.isTouched())
        {
          lstGraphics.remove(graphic);
        }
       }
    }
   }
  
   @Override
      public void onDraw(Canvas canvas) {
           /// draw the backgrounds and each element from lstGraphics
  }
  
  public class Graphic {
  
          private Bitmap bitmap;
              private boolean touched;
              private Coordinates initialCoordinates; 
  ....
  }
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>&gt; 03-01 10:01:53.365: ERROR/AndroidRuntime(454): Uncaught handler: thread Thread-12 exiting due to uncaught exception 
  &gt; 03-01 10:01:53.365: ERROR/AndroidRuntime(454): java.util.ConcurrentModificationException
  &gt; 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:66)
  &gt; 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at com.test.customcontrols.Panel.updatePhysics(Panel.java:290)
  &gt; 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at com.test.customcontrols.AnimationHideThread.run(AnimationHideThread.java:41)
  </code></pre>
  
  <p>Any help is greatly appreciated. Thank you.</p>