我有简单的TimerHandler,可以替换一些Sprite(Place extends Sprite)。
我的代码
ArrayList<Place> itemsToVisit = new ArrayList<Place>();
public void Somemethod(){}
TimerHandler effectTimer = new TimerHandler(3f, true, new ITimerCallback() {
@Override
public void onTimePassed(final TimerHandler pTimerHandler) {
delegate.RequestEntity().unregisterUpdateHandler(pTimerHandler);
updateOnEffect = new Handler(Looper.getMainLooper());
updateOnEffect.post(new Runnable() {
@Override
public void run() {
for (Place l : itemsToVisit) {
if (youAreThePlace(l)) {
int indexForChange = itemsToVisit.indexOf(l);
swapPlace(l, indexForChange);
}
}
}
}
});
}
});
delegate.RequestEntity().registerUpdateHandler(effectTimer);
}
public void swapPlace(Place l, int indexForChange) {
((Scene) delegate.RequestEntity().getParent()).unregisterTouchArea(l);
l.detachSelf();
l = createNewPlace(l, false);
itemsToVisit.set(indexForChange, l);
delegate.RequestEntity().attachChild(l);
((Scene) delegate.RequestEntity().getParent()).registerTouchArea(l);
}
有时它会在崩溃之前工作4次,有时候是在第一次运行之后。
错误
java.lang.IndexOutOfBoundsException: Invalid index 40, size is 40
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at org.andengine.entity.Entity.onManagedDraw(Entity.java:1635)
at org.andengine.entity.Entity.onDraw(Entity.java:1398)
at org.andengine.entity.Entity.onManagedDraw(Entity.java:1635)
at org.andengine.entity.scene.Scene.onManagedDraw(Scene.java:259)
at org.andengine.entity.Entity.onDraw(Entity.java:1398)
at org.andengine.engine.Engine.onDrawScene(Engine.java:647)
at org.andengine.engine.Engine.onDrawFrame(Engine.java:637)
at org.andengine.opengl.view.EngineRenderer.onDrawFrame(EngineRenderer.java:105)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
我不知道自己做错了什么?当我用activity.runOnUpdateThread((new Runnable() {
替换处理程序时
然后在第一次运行中我得到关于Looper.prepare()
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.widget.Toast$TN.<init>(Toast.java:322)
at android.widget.Toast.<init>(Toast.java:91)
at android.widget.Toast.makeText(Toast.java:238)
at pl.newaxis.szyfraki.Controller.LetterManager$1$1$1.run(PlaceManager.java:51)
at org.andengine.engine.handler.runnable.RunnableHandler.onUpdate(RunnableHandler.java:42)
at org.andengine.engine.Engine.onUpdateUpdateHandlers(Engine.java:617)
at org.andengine.engine.Engine.onUpdate(Engine.java:605)
at org.andengine.engine.Engine.onTickUpdate(Engine.java:568)
at org.andengine.engine.Engine$UpdateThread.run(Engine.java:858)
你能告诉我我做错了吗?
我不得不将仅存在activity.runOnUpdateThread的项目分离。所以最终swapPlace看起来像这样
public void swapPlace(Place l, int indexForChange) {
((Scene) delegate.RequestEntity().getParent()).unregisterTouchArea(l);
activity.runOnUpdateThread(new Runnable() {
@Override
public void run() {
finalL.detachSelf();
}
});
l = createNewPlace(l, false);
itemsToVisit.set(indexForChange, l);
delegate.RequestEntity().attachChild(l);
((Scene) delegate.RequestEntity().getParent()).registerTouchArea(l);
}
答案 0 :(得分:1)
这个错误几乎总是因为在updateThread之外删除了Sprite。当您将其删除时,索引无法正确更新,并且下次updateThread尝试刷新内容时 - 缺少某些内容,因此indexOutofBounds。
有关如何确保删除updateThread上的Sprite的一些提示,请参阅此问题/答案java.lang.IndexOutOfBoundsException: Invalid index 13, size is 13。