【问题标题】:how to deal with ava.lang.IndexOutOfBoundsException: Invalid index 4, size is 4如何处理 ava.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
【发布时间】:2017-08-10 00:59:56
【问题描述】:

我正在制作一个测验应用程序,但遇到以下错误 我的 logcat 如下

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.example.owner.quiz, PID: 25307
                                                                    java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4
                                                                        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                        at java.util.ArrayList.get(ArrayList.java:308)
                                                                        at com.example.owner.quiz.MainActivity$1.onClick(MainActivity.java:50)
                                                                        at android.view.View.performClick(View.java:4791)
                                                                        at android.view.View$PerformClick.run(View.java:19884)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5268)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

当我点击(MainActivity.java:50) 时,它将转到主要活动 并显示currentQ=quesList.get(qid); 这个 以下是我的主要活动代码

public class MainActivity extends Activity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DbHelper db=new DbHelper(this);
    quesList=db.getAllQuestions();
    currentQ=quesList.get(qid);
    txtQuestion=(TextView)findViewById(R.id.textView1);
    rda=(RadioButton)findViewById(R.id.radio0);
    rdb=(RadioButton)findViewById(R.id.radio1);
    rdc=(RadioButton)findViewById(R.id.radio2);
    butNext=(Button)findViewById(R.id.button1);



    setQuestionView();


    butNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
            if(currentQ.getANSWER().equals(answer.getText()))
            {
                score ++ ;
            }
            currentQ=quesList.get(qid);
            setQuestionView();
        }
    });
}

谁能告诉我如何处理这个问题

【问题讨论】:

标签: android sqlite indexoutofboundsexception


【解决方案1】:

您尝试使用 if 进行验证,参见示例

if (qid < quesList.size()) {
  currentQ=quesList.get(qid);
}

【讨论】:

    【解决方案2】:

    您正在将 qid 的值更改为 4,而该值不在您包含的代码中,但您的 quesList 只有 4 项长,因此它的有效索引是 0-3

    【讨论】:

      【解决方案3】:

      这可能是由于 sqlite 索引从 1 开始(而不是像 List 那样从 0 开始),你能显示你的 DbHelper 类代码吗?特别是 getAllQuestions() 方法。

      【讨论】:

      • 公共列表 getAllQuestions() { List quesList = new ArrayList();字符串 selectQuery = "SELECT * FROM " + TABLE_QUEST; dbase=this.getReadableDatabase();光标 cursor = dbase.rawQuery(selectQuery, null);
      • if (cursor.moveToFirst()) { do { Question quest = new Question(); quest.setID(cursor.getInt(0)); quest.setQUESTION(cursor.getString(1)); quest.setANSWER(cursor.getString(2)); quest.setOPTA(cursor.getString(3)); quest.setOPTB(cursor.getString(4)); quest.setOPTC(cursor.getString(5)); questList.add(任务); } while (cursor.moveToNext()); }
      • 好的,这看起来没问题,你会从这个方法中得到一个具有一定大小的“问题”列表。请注意,您将问题的 ID 设置为光标中的 0 位置。我假设这是您的自动增量 ID,如果我错了,请纠正我,此自动增量从值 1 开始。现在我需要查看您是否使用此 ID 从 MainActivity 中的 qustion 列表中获取问题索引。告诉我你在主要活动中更改“qid”值的代码。
      猜你喜欢
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 2018-08-01
      • 2020-02-01
      • 1970-01-01
      • 2017-02-03
      • 2016-03-30
      • 1970-01-01
      相关资源
      最近更新 更多