android - java.lang.IndexOutOfBoundsException:无效的索引2,大小为2

标签 android crash andengine crash-reports

在使用andengine开发的我的应用程序中,某些用户过于频繁地崩溃。
我没有分离/附加任何对象,但是仍然出现此错误。我正在使用对象可见性和unregistertoucharea,尽管我没有将其放到runonupdate中。会导致这个问题吗?如果不能,请告诉我可能如何?

这是我从Google Play获得的崩溃报告。

java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at org.andengine.c.a.d(Unknown Source)
at org.andengine.c.c.e.d(Unknown Source)
at org.andengine.c.a.a(Unknown Source)
at org.andengine.b.a.b(Unknown Source)
at org.andengine.b.a.a(Unknown Source)
at org.andengine.opengl.e.g.onDrawFrame(Unknown Source)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

有时游戏在游戏结束时崩溃,有时在重新启动游戏时崩溃

游戏结束时,我正在调用以下函数
public void GameOver(){
    yourScoreText.setText(""+score);        
    restart_button.setVisible(true);
    leaderboard_button.setVisible(true);
    share_button.setVisible(true);
    gameOverText.setVisible(true);
    gameOverOverlay.setVisible(true);
    yourScoreText.setVisible(true);
    highScoreText.setVisible(true);

    this.registerTouchArea(restart_button);
    this.registerTouchArea(leaderboard_button);
    this.registerTouchArea(share_button);
}

当按下重启键时,我正在调用以下函数。
public void RestartGame(){
    restart_button.setVisible(false);
    leaderboard_button.setVisible(false);
    share_button.setVisible(false);
    gameOverText.setVisible(false);
    gameOverOverlay.setVisible(false);
    yourScoreText.setVisible(false);
    highScoreText.setVisible(false);
    this.unregisterTouchArea(restart_button);
    this.unregisterTouchArea(leaderboard_button);
    this.unregisterTouchArea(share_button);
    score = 0;
    scoreText.setText("Score: "+score);

    Log.d("game start","game has been started");
    StartScreen();
}
public void StartScreen(){
    leaderboard_home_button.setVisible(true);
    rate_button.setVisible(true);
    if(activity.getNoAds()==0){
        no_ads_button.setVisible(true);
        this.registerTouchArea(no_ads_button);
    }
    if(isMute){
        sound_off_button.setVisible(true);
        this.registerTouchArea(sound_off_button);
    } else {
        sound_on_button.setVisible(true);
        this.registerTouchArea(sound_on_button);
    }
    this.registerTouchArea(leaderboard_home_button);
    this.registerTouchArea(rate_button);

}

最佳答案

数组索引从0开始计数。
因此,如果Array的大小= 2,则它们具有以下索引:0,1。因此,在这种情况下,索引号2不存在。

PS:如果您需要更多详细信息,请提供一些代码,我将编辑我的答案;)

关于android - java.lang.IndexOutOfBoundsException:无效的索引2,大小为2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26797183/

相关文章:

crash - Strtol和Glib缓冲区导致崩溃

android - 当全局静态变量被杀死android时重启应用程序

memory-leaks - 应用程序将日志显示为收到内存警告并在 uiscrollview 中更快地滚动图像时崩溃?

android - 尝试启动并运行 andengine

android - 使用两个相同的 Sprite 对象和引擎进行碰撞检测?

android - Glide 有时不起作用,但有时却起作用

android - 无法将我的 apk 上传到 Play 商店

android - 如何在Android Studio中添加libandenginephysicsbox2dextension.so文件|和引擎

Android蓝牙多连接问题?

java - 如何理解显示LeakCanary?