我不知道这是否是寻求帮助的正确方法,但我最近将应用程序上传到了Google Play商店。这是指向my app
的链接提交的错误:
java.util.ConcurrentModificationException
at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
at de.krissini.server.GamePanel.onDraw(GamePanel.java:230)
at android.view.View.draw(View.java:15249)
at android.view.SurfaceView.draw(SurfaceView.java:340)
at android.view.View.updateDisplayListIfDirty(View.java:14185)
at android.view.View.getDisplayList(View.java:14207)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
at android.view.View.updateDisplayListIfDirty(View.java:14145)
at android.view.View.getDisplayList(View.java:14207)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
at android.view.View.updateDisplayListIfDirty(View.java:14145)
at android.view.View.getDisplayList(View.java:14207)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3391)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3370)
at android.view.View.updateDisplayListIfDirty(View.java:14145)
at android.view.View.getDisplayList(View.java:14207)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2560)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2382)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2012)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1084)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5990)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
现在需要更多文字blablabla对不起,请不要读这个
答案 0 :(得分:1)
这将有助于您: 它创建了集合的浅表副本,并重复了副本而不是原始副本。
@Override
public void onDraw(Canvas canvas) {
if (canvas != null && getHolder().getSurface().isValid()) {
bg.draw(canvas);
player.draw(canvas);
if (player.getPlaying()) {
List<Smokepuff> copySmoke = new ArrayList<Smokepuff>(smoke);
List<Missle> copyMissles = new ArrayList<Missle>(missles);
for (Smokepuff sp : copySmoke) {
sp.draw(canvas);
}
for (Missle m : copyMissles) {
m.draw(canvas);
}
drawScore(canvas);
}
postInvalidate();
}
}
然而你应该想到为什么它首先崩溃在这里。
当您尝试在迭代时修改集合时会发生 ConcurrentModificationException
。你在多个线程中使用该集合吗?如果是,您应该考虑一些同步。特别是在添加和删除操作时。
onDraw()
会经常被调用,因此,由于性能问题,在每次调用的每次调用中制作集合副本可能不是一个好主意。它会对你有所帮助,但我仍然建议检查你在不同线程中使用这些集合的所有地方。
答案 1 :(得分:0)
@Override
public void onDraw(Canvas canvas) {
if (canvas != null && getHolder().getSurface().isValid()) {
bg.draw(canvas);
player.draw(canvas);
if (player.getPlaying()) {
for (Smokepuff sp : smoke) {
sp.draw(canvas);
}
for (Missle m : missles) {
m.draw(canvas);
}
drawScore(canvas);
}
postInvalidate();
}
}