java.lang.IndexOutOfBoundsException: Invalid index 13, size is 13

时光总嘲笑我的痴心妄想 提交于 2019-12-19 22:08:52

问题


Im getting some weird error which crashing my android app. Its a quiz app. So when user answer 2/3 question correctly then click the next button this it crash. and show index 13 error. But i couldn't figure out where to fix/ look for. 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;
            }
        }
    }
}   

Log cat details is like this

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 full class file http://jquery404.com/file/GameScene.txt


回答1:


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

The actual error is being thrown from some code within the library you are using

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

It is likely that you are changing the list in a separate thread whilst the library is also interacting with the list.


From the 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);
            }
    }

As this is running in a separate thread, it is likely that you are removing an object from the children ArrayList while the loop is iterating. To fix this you should call your changes to the children ArrayList like jmr499485 explains in his answer.

java.lang.IndexOutOfBoundsException: Invalid index 13, size is 13

The only item in your code I can see that would be causing this is the statement questionText.detachSelf(); which you have used in many places. You should instead use;

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



回答2:


This typically happens when you remove something from the screen and don't do it on the updateThread. Make sure that any place you are removing items that you call it like this

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

See this similar question - How can repair this error? "java.lang.IndexOutOfBoundsException"



来源:https://stackoverflow.com/questions/18148097/java-lang-indexoutofboundsexception-invalid-index-13-size-is-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!