Java.lang.IndexOutOfBoundsException: Invalid index 13, size 13

I get some strange error that is harming my Android app. This is a quiz app. Therefore, when the user answers question 2/3 correctly, click the next button to collapse it. and show index error 13. But I could not figure out where to fix / look. here is my code snippet.

public Vector<Sprite> defaultTile; private void GameResults() { if(result.equals("right")) { GameOver(); Log.e("Gaa", "Right Here ->"); } } private void GameOver() { { for (int i = 0; i < defaultTile.size(); i++) { defaultTile.get(i).setVisible(false); } for (int i = 0; i < defaultTile.size(); i++) { unregisterTouchArea(defaultTile.get(i)); } questionText.detachSelf(); } @Override public boolean onAreaTouched(TouchEvent event, ITouchArea area, float posX, float posY) { if(event.isActionUp()) { if(area instanceof Sprite) { Sprite sprite = (Sprite)area; int userData = (Integer) sprite.getUserData(); switch(userData) { case BTN_NEXT: if(gameState.equals("alpha") && tickBg.isVisible()) { countdown.cancel(); GameResults(); } break; } } } } 

Feline details are similar to

 08-09 13:30:50.246: W/dalvikvm(919): threadid=12: thread exiting with uncaught exception (group=0x409c01f8) 08-09 13:30:50.276: E/AndroidRuntime(919): FATAL EXCEPTION: GLThread 08-09 13:30:50.276: E/AndroidRuntime(919): java.lang.IndexOutOfBoundsException: Invalid index 9, size is 9 08-09 13:30:50.276: E/AndroidRuntime(919): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 08-09 13:30:50.276: E/AndroidRuntime(919): at java.util.ArrayList.get(ArrayList.java:304) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.entity.Entity.onManagedDrawChildren(Entity.java:1008) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.entity.Entity.onDrawChildren(Entity.java:1000) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.entity.Entity.onManagedDraw(Entity.java:993) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.entity.shape.Shape.onManagedDraw(Shape.java:120) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.entity.Entity.onDraw(Entity.java:875) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.entity.Entity.onManagedDrawChildren(Entity.java:1008) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.entity.Entity.onDrawChildren(Entity.java:1000) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.entity.Entity.onManagedDraw(Entity.java:993) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.entity.scene.Scene.onManagedDraw(Scene.java:233) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.entity.Entity.onDraw(Entity.java:875) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.engine.Engine.onDrawScene(Engine.java:517) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.engine.Engine.onDrawFrame(Engine.java:509) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.opengl.view.RenderSurfaceView$Renderer.onDrawFrame(RenderSurfaceView.java:154) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:617) 08-09 13:30:50.276: E/AndroidRuntime(919): at org.anddev.andengine.opengl.view.GLSurfaceView$GLThread.run(GLSurfaceView.java:549) 

UPDATE

Here is my complete class file http://jquery404.com/file/GameScene.txt

+4
source share
2 answers

In programming, indexes often start at 0, so if you have 9 elements, the highest index will be 8.

The actual error is thrown from some code in the library that you are using

 org.anddev.andengine.entity.Entity.onManagedDrawChildren(Entity.java:1008) 

You are probably changing the list in a separate thread, while the library also interacts with the list.


From gcode project ;

  public void onManagedDrawChildren(final Camera pCamera) { final ArrayList<IEntity> children = this.mChildren; final int childCount = children.size(); for(int i = 0; i < childCount; i++) { children.get(i).onDraw(pCamera); } } 

Since this runs in a separate thread, you are likely to remove the object from the children of the ArrayList while the loop iterates. To fix this, you should call your changes to the ArrayList children, as jmr499485 explains in your answer.

java.lang.IndexOutOfBoundsException: invalid index 13, size 13

The only element in the code that I see will be the reason that this is a questionText.detachSelf(); statement questionText.detachSelf(); which you used in many places. Instead you should use;

 runOnUpdateThread(new Runnable() { @Override public void run() { questionText.detachSelf(); } }); 
+8
source

This usually happens when you delete something from the screen and do not do it on updateThread. Make sure that anywhere you delete items that you call so

 runOnUpdateThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub //remove/detach your stuff in here } }); 

See this similar question - How to recover this error? "java.lang.IndexOutOfBoundsException"

+2
source

Source: https://habr.com/ru/post/1496153/


All Articles