Android java.lang.IndexOutOfBoundsException:索引2无效,大小为2
有些用户在我用安德林开发的应用程序中经常遇到这种崩溃。 我没有分离/附加任何对象,但仍然会出现此错误。我正在使用对象可见性和unregistertoucharea,虽然我没有将其放入runonupdate。这会导致这个问题吗。如果没有,有人能告诉我可能发生什么吗 这是我从Google Play得到的崩溃报告Android java.lang.IndexOutOfBoundsException:索引2无效,大小为2,android,crash,andengine,crash-reports,Android,Crash,Andengine,Crash Reports,有些用户在我用安德林开发的应用程序中经常遇到这种崩溃。 我没有分离/附加任何对象,但仍然会出现此错误。我正在使用对象可见性和unregistertoucharea,虽然我没有将其放入runonupdate。这会导致这个问题吗。如果没有,有人能告诉我可能发生什么吗 这是我从Google Play得到的崩溃报告 java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 at java.util.ArrayList.throwInd
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)
有时游戏在gameover上崩溃,有时在重新启动游戏时崩溃
游戏结束后,我调用以下函数
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开始计数。 所以,如果数组的大小=2,它就有以下索引:0,1。因此,在本例中,索引号2不存在
PS:如果您需要更多详细信息,请提供一些代码,我将编辑我的答案;) 我认为问题在于初始化文本对象。 初始化时,需要设置其最大长度:
private Text highScoreText = new Text(SOME_X, SOME_Y, resourceManager.font, "New Best: 1234567890", new TextOptions(HorizontalAlign.CENTER), vbom);
不要忘记对Score文本对象执行同样的操作 更新了问题。这不是我定义/使用数组的问题。这是安卓/安卓系统方面正在发生的事情。崩溃只是偶尔发生,但这是我从用户那里得到的最大崩溃次数。我实际上不知道andengine,但我知道的是抛出异常的含义。我可以确保您的问题是不在数组中的索引(异常的名称告诉您这一点)。如果不是您的代码,要么您没有在某个地方为您的框架提供正确的信息(比如声明数组大小?),要么框架有一个bug,您可以告诉开发人员;)代码中的哪一行给出了此异常?你能告诉我吗?我几乎肯定是.setText方法,是吗?你是如何初始化文本对象的?不知道是哪一行导致了异常。下面是我如何定义文本对象的@ranifisch私有文本highScoreText;然后在CreateSecene函数中highScoreText=新文本(一些是X,一些是Y,resourceManager.font,“新最佳:1234”,新文本选项(HorizontalAlign.CENTER),vbom);当它崩溃时,分数是多少?但分数永远不会超过3位数。所以我认为这不应该是一个问题。也检查其他文本对象的初始化。