1

我无法从另一个类访问我的 ArrayList。我正在关注过去的示例和互联网示例,但似乎无法使其正常工作。

我发布了我的 3 个课程只是为了彻底。错误在我的 QuestionView.java 类中。我在我的代码中记录了错误消息的位置和内容。

感谢您对我的代码的任何其他部分提出的任何其他建议。:)

QuestionView.java

public class QuestionView extends Activity {

    Quiz q = new Quiz();
    ArrayList<Question> queries = new ArrayList<Question>();
    queries.getTenQs();  //error here:  "Syntax error on token "getTenQs", Identifier expected after this token"

@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());
        answer1.setText(queries.get(i).getA1());
        answer2.setText(queries.get(i).getA2());
        answer3.setText(queries.get(i).getA3());
        answer4.setText(queries.get(i).getA4());

                    //more code...
    }
}
  }

测验.java

  package com.example.test;

  import java.util.ArrayList;

  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;
}
  }

问题.java

  package com.example.test;

  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; }
  }
4

1 回答 1

5

您调用 getTenQs() 就好像它是 ArrayList 中的一个方法。我想你想要的是:

Quiz q = new Quiz();
ArrayList<Question> queries = q.getTenQs();  

希望有帮助!

于 2012-11-30T15:27:37.137 回答