我錯過了Android中同步代碼的概念。Android動畫中的java.util.ConcurrentModificationException
方案
總有一些在屏幕上繪製的3項。每個圖像都存儲在ArrayList(lstGraphics)中。爲此我使用SurfaceView。一旦用戶點擊圖像,圖像獲取的市場將被刪除,並將添加一個新的。
代碼示例:
AnimationHideThread
...
@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);
}
}
}
}
...
所以你可以先看起來我updatePhysics()。這意味着我計算每個圖像移動的方向。在這裏,我還將刪除列表中的點擊圖片。之後,我檢查是否需要在manageAnimations()的列表中添加一個新項目,然後最後一步繪製整個項目。
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
....
public void manageAnimations()
{
synchronized (this.getHolder()) {
...
while (lstGraphics.size()<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;
....
}
我得到的錯誤是:
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): Uncaught handler: thread Thread-12 exiting due to uncaught exception
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): java.util.ConcurrentModificationException
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at java.util.AbstractList$SimpleListIterator.next(AbstractList.java:66)
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at com.test.customcontrols.Panel.updatePhysics(Panel.java:290)
> 03-01 10:01:53.365: ERROR/AndroidRuntime(454): at com.test.customcontrols.AnimationHideThread.run(AnimationHideThread.java:41)
任何幫助是極大的讚賞。謝謝。
我創建的列表的文檔,刪除和測試它,工作就像一個魅力。感謝您的幫助。 – Alin 2011-03-01 09:04:54
@Alin你應該考慮'Iterator'的解決方案。在遊戲循環中創建不必要的對象通常是一個壞主意。 – 2011-03-01 11:07:49