0

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)

任何帮助或输入表示赞赏!先感谢您。

4

3 回答 3

1

将您的 for 循环条件更改为:

for(int i = 0; i < queries.size() ; i++)

并确保queries在使用之前初始化 ArrayList

于 2012-11-30T17:15:02.587 回答
0

您不会调用您的getRandom10()方法来初始化列表。

编辑
你还使用了糟糕的设计类Quiz

  1. tenQsstatic您用 list 中的数据填充的non-static列表qList

  2. tenQs可以public通过 class: 直接访问它Quiz.tenQs。我想制作它private并在内部non-static调用getRandom10conturtor 以确保正确初始化您的Quiz.

  3. 将您的随机索引设为:

代码:

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将不会执行,因为实际大小为tenQsis0但容量为10。因此,您必须在测验中创建具有问题数量的常数并在此for循环中使用它。

于 2012-11-30T17:14:26.360 回答
0

qList.size()+1 = 11 所以 random 会给出 0 到 10 之间的任何数字...所以如果数字是从 0 到 9 很好,但是 10 会导致您的程序失败...。

于 2012-11-30T17:15:11.800 回答