Android 安卓:我在代码序列中有一些错误

Android 安卓:我在代码序列中有一些错误,android,Android,我对这个代码序列有问题 question= Common.questionList.get(questionIndex); 如果我对这一行进行注释,我会在第292行得到一个错误 txt_wrong_answer.setText(String.valueOf(0)); 如果我也对它发表评论,程序会运行,但无法完成游戏。 替换此问题=Common.questionList.get(questionIndex);到下面的代码 if(questionIndex >= 0 &&

我对这个代码序列有问题

question= Common.questionList.get(questionIndex);
如果我对这一行进行注释,我会在第292行得到一个错误

txt_wrong_answer.setText(String.valueOf(0));
如果我也对它发表评论,程序会运行,但无法完成游戏。
替换此问题=Common.questionList.get(questionIndex);到下面的代码

if(questionIndex >= 0 && questionIndex < Common.questionList.size()){
     question= Common.questionList.get(questionIndex);
}
if(questionIndex>=0&&questionIndex
对于第一个错误,您正在访问数组的-1。没有这样的位置,因此会引发
ArrayOutOfBounds
异常。我会将默认值0设置为-1。
questionIndex=getArguments().getInt(“index”,0)
我还建议检查或确保
questions
数组至少有一个元素。因为如果它是空的,0也会抛出相同的错误


对于第二个错误,您的参考
txt\u错误\u答案
尚未在
onprepareOptions功能表
中设置。确保在调用
.setText
之前设置它,或者使用if块
if(txt\u error\u answer!=null)

在292上得到什么错误?第一个错误是因为索引是-1,并且数组中没有这样的位置。尝试将默认值0设置为非-1。我将值设置为0:没有参考集
txt\u error\u answer
null。显然,信息是这样说的。你在哪里设置了错误的答案?谢谢你的回答,但是他没有帮我