LogCat输出中的错误指向QuestionView.java的第37行,我将在下面的代码中添加注释。下面有4个课程的4个摘要。我也有LogCat的输出。
MainMenu.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...
Question.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; }
}
Quiz.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...
LogCat
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)
任何帮助或输入表示赞赏!先感谢您。
最佳答案
您无需调用getRandom10()
方法来初始化列表。
编辑
同样,您对类Quiz
使用了错误的设计:tenQs
是您用static
列表non-static
中的数据填充的qList
列表。tenQs
也是public
,可以直接通过类Quiz.tenQs
访问它。我想将其设置为private
和non-static
,并在构造函数内部调用getRandom10
,以确保正确地初始化Quiz
。
得到你的随机索引为:
码:
Random r = new Random();
for(int i = 0; i < 10; i++) {
Question x = qList.get(r.nextInt(qList.size()));
tenQs.add(x);
}
我明白了如果在
for
条件< tenQs.size()
中使用此for
,则不会执行该tenQs
,因为0
的实际大小为10
,但容量为for
。因此,您必须在测验中创建带有多个问题的常量,并在此循环中使用它。