在这里,我在 for(Candy c2 : candylist) {
行中收到 java.util.ConcurentModificationError
。
谁能告诉我出了什么问题吗?我正在尝试检查两个链接列表中管理的多个图像的碰撞检测
- 第一个气球列表
- 第二次火灾
这段代码是气球和火的碰撞检测:
public void collision(final Balloon mb1) {
ImageView iv11 = mb1.getIv1();
Rect r1 = new Rect(iv11.getLeft(), iv11.getTop(), iv11.getLeft() + iv11.getWidth(), iv11.getTop()+iv11.getHeight());
for (Candy c2 : candylist) {
if (c2 != null) {
ImageView iv22 = c2.getIv2();
if (iv22 != null) {
Rect r2 = new Rect(iv22.getLeft(), iv22.getTop(), iv22.getLeft() + iv22.getWidth(), iv22.getTop()+iv22.getHeight());
if (r1.intersect(r2)) {
runOnUiThread(new Runnable() {
public void run() {
mb1.getTt1().cancel();
if (mb1.isBursted() == false) {
mb1.setBursted(true);
update(mb1);
}
}
});
}
}
}
}
}
public void update(final Balloon mb1) {
final int bp = balloonList.indexOf(mb1);
if (bp != -1) {
if (sound == true) {
mp = MyUtils.playSound(R.raw.bur, Game.this);
}
final ImageView iv1 = mb1.getIv1();
iv1.setImageResource(R.drawable.negate);
iv1.setEnabled(false);
Animation a1 = AnimationUtils.loadAnimation(Game.this, R.anim.fade);
iv1.startAnimation(a1);
TimerTask tt = mb1.getTt1();
tt.cancel();
TimerTask tt1 = new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
balloonList.set(bp, null);
al.removeView(iv1);
}
});
}
};
t1.schedule(tt1, 1200);
missed++;
score = score-10;
if (music == true) {
if (mp2 != null) {
MyUtils.releaseSound(mp2);
}
}
showscore();
showExitDialog(score);
}
}
堆栈跟踪:
03-08 00:07:14.326: E/AndroidRuntime(586): FATAL EXCEPTION: Timer-1
03-08 00:07:14.326: E/AndroidRuntime(586): java.util.ConcurrentModificationException
03-08 00:07:14.326: E/AndroidRuntime(586): at java.util.LinkedList$LinkIterator.next(LinkedList.java:124)
03-08 00:07:14.326: E/AndroidRuntime(586): at bitcream.candyhive.candy.Game.collision(Game.java:609)
03-08 00:07:14.326: E/AndroidRuntime(586): at bitcream.candyhive.candy.Game$11.run(Game.java:229)
03-08 00:07:14.326: E/AndroidRuntime(586): at java.util.Timer$TimerImpl.run(Timer.java:284)
最佳答案
您的集合 candylist
在您迭代时会被修改。
假设您的集合是线程安全的,您应该在迭代它之前创建该集合的副本。
for (Candy c2 : candylist.toArray(new Candy[0]))
关于java - 在碰撞检测中获取 java.util.ConcurrentModificationError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22258330/