LogCat 输出的错误指向 QuestionView.java 的第 37 行,我将在下面的代码中对其进行注释。下面我有 4 个类的 4 个片段。我也有来自 LogCat 的输出。
主菜单.java
public class MainMenu extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenumain);
Button button1 = (Button)findViewById(R.id.startBtn);
Button button2 = (Button)findViewById(R.id.categoriesBtn);
Button button3 = (Button)findViewById(R.id.highscoresBtn);
Button button4 = (Button)findViewById(R.id.aboutBtn);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent1 = new Intent(MainMenu.this, QuestionView.class);
startActivity(intent1);
}
});
//more code...
问题.java
public class Question {
String a1;
String a2;
String a3;
String a4;
int correctAnswer;
String query;
public Question() {
}
public Question(String a1, String a2, String a3, String a4, int correctAnswer, String query) {
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
this.a4 = a4;
this.correctAnswer = correctAnswer;
this.query = query;
}
public String getA1() { return a1; }
public String getA2() { return a2; }
public String getA3() { return a3; }
public String getA4() { return a4; }
public String getQuery() { return query; }
public int getCorrectAnswer() { return correctAnswer; }
}
测验.java
public class Quiz {
ArrayList<Question> qList = new ArrayList<Question>();
public static ArrayList<Question> tenQs = new ArrayList<Question>(10);
public Quiz() {
qList.add(new Question("A", "B", "C", "D", 3, "Question 1?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 2?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 3?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 4?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 5?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 6?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 7?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 8?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 9?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 10?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 11?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 12?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 13?"));
qList.add(new Question("A", "B", "C", "D", 3, "Question 14?"));
}
public void getRandom10() {
for(int i = 0; i < 10; i++) {
Question x = qList.get((int) Math.floor((qList.size()+1)*Math.random()));
if(tenQs.contains(x) == true) {
i--;
} else {
tenQs.add(x);
}
}
}
public ArrayList<Question> getTenQs() {
return tenQs;
}
}
QuestionView.java
public class QuestionView extends Activity {
Quiz quiz = new Quiz();
ArrayList<Question> queries = quiz.getTenQs();
int correctAnswers = 0;
int wrongAnswers = 0;
int answer = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionviewmain);
TextView question = (TextView)findViewById(R.id.question);
Button answer1 = (Button)findViewById(R.id.answer1);
Button answer2 = (Button)findViewById(R.id.answer2);
Button answer3 = (Button)findViewById(R.id.answer3);
Button answer4 = (Button)findViewById(R.id.answer4);
for(int i = 0; i < 10; i++) {
question.setText(queries.get(i).getQuery()); //Error points to here.
answer1.setText(queries.get(i).getA1());
answer2.setText(queries.get(i).getA2());
answer3.setText(queries.get(i).getA3());
answer4.setText(queries.get(i).getA4());
answer = queries.get(i).getCorrectAnswer();
answer1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(answer == 0) {
correctAnswers++;
} else {
wrongAnswers++;
}
}
});
//more code...
日志猫
11-12 00:08:40.983: E/AndroidRuntime(4338): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.QuestionView}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-12 00:08:40.983: E/AndroidRuntime(4338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-12 00:08:40.983: E/AndroidRuntime(4338): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-12 00:08:40.983: E/AndroidRuntime(4338): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-12 00:08:40.983: E/AndroidRuntime(4338): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-12 00:08:40.983: E/AndroidRuntime(4338): at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 00:08:40.983: E/AndroidRuntime(4338): at android.os.Looper.loop(Looper.java:137)
11-12 00:08:40.983: E/AndroidRuntime(4338): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-12 00:08:40.983: E/AndroidRuntime(4338): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 00:08:40.983: E/AndroidRuntime(4338): at java.lang.reflect.Method.invoke(Method.java:511)
11-12 00:08:40.983: E/AndroidRuntime(4338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-12 00:08:40.983: E/AndroidRuntime(4338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 00:08:40.983: E/AndroidRuntime(4338): at dalvik.system.NativeStart.main(Native Method)
11-12 00:08:40.983: E/AndroidRuntime(4338): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-12 00:08:40.983: E/AndroidRuntime(4338): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-12 00:08:40.983: E/AndroidRuntime(4338): at java.util.ArrayList.get(ArrayList.java:304)
11-12 00:08:40.983: E/AndroidRuntime(4338): at com.example.test.QuestionView.onCreate(QuestionView.java:37)
11-12 00:08:40.983: E/AndroidRuntime(4338): at android.app.Activity.performCreate(Activity.java:5008)
11-12 00:08:40.983: E/AndroidRuntime(4338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-12 00:08:40.983: E/AndroidRuntime(4338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
任何帮助或输入表示赞赏!先感谢您。