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

前端 未结 2 1201
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-19 19:28

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 inde

相关标签:
2条回答
  • 2021-01-19 19:40

    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"

    0 讨论(0)
  • 2021-01-19 19:48

    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();
    }
    });
    
    0 讨论(0)
提交回复
热议问题