尝试在Android应用程序中随机化几个问题时出错

时间:2016-04-29 14:06:30

标签: java android

问候窥视,需要有关此错误的帮助。尝试谷歌搜索,但没有解决问题。我确实把错误发生在哪一行。

注意下面的代码有2个不同的java类

[IMG = “屏幕截图”] http://i.imgur.com/XYFHZOz.png[/IMG]

public class QuestionAndAnswer {
public List<String> allAnswers; // distractors plus real answer
public String answer;
public String question;
public String selectedAnswer;
public int selectedId = -1;

public QuestionAndAnswer(String question, String answer, List<String> distractors) {
    this.question = question;
    this.answer = answer;
    allAnswers = new ArrayList<String>(distractors);

    // Add real answer to false answers and shuffle them around
    allAnswers.add(answer);
    Collection.shuffle(allAnswers); //error on this line
}

public boolean isCorrect() {
    return answer.equals(selectedAnswer);
}

}

public class UjiSalah extends Activity {
RadioGroup rg;
int currentQuestion = 0;
TextView tv;
List<QuestionAndAnswer> quiz = new ArrayList<QuestionAndAnswer>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.uji_salah);

    tv = (TextView) findViewById(R.id.tvque);
    rg = (RadioGroup) findViewById(R.id.radioGroup1);

    // Setup a listener to save chosen answer
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId > -1) {
                QuestionAndAnswer qna = quiz.get(currentQuestion);
                qna.selectedAnswer = ((RadioButton) group.findViewById(checkedId)).getText().toString();
                qna.selectedId = checkedId;
            }
        }
    });

    String[] question = {"Bilangan Rokaat Solah Zuhur ?","Bilangan Rokaat Solat Subuh ?","Bilangan Rokaat Solat Maghrib ?"};
            String[] answer = { "4 rokaat","2 rokaat","3 rokaat" };
            String[] distractor = { "5 rokaat","1 rokaat","4 rokaat","2 rokaat","3 rokaat","4 rokaat","4 rokaat","5 rokaat","3 rokaat" };

            ArrayList<String> distractorList = Arrays.asList(distractor); //error on this line


    int length = question.length;
    for(int i = 0; i < length; i++)
        quiz.add(new QuestionAndAnswer(question[i], answer[i], distractorList.subList(i * 3, (i + 1) * 3)));
    Collection.shuffle(quiz); //error here

    fillInQuestion();
    }

    public void fillInQuestion() {
        QuestionAndAnswer qna = quiz.get(currentQuestion);
        tv.setText(qna.question);

        // Set all of the answers in the RadioButtons
        int count = rg.getChildCount();
        for(int i = 0; i < count; i++)
            ((RadioButton) rg.getChildAt(i)).setText(qna.allAnswers.get(i));

        // Restore selected answer if exists otherwise clear previous question's choice
        if(qna.selectedId > -1)
            rg.check(qna.selectedId);
        else
            rg.clearCheck();
    }
}

2 个答案:

答案 0 :(得分:1)

+ 1 @ Prera​​k Sola,您可以将allAnswers声明为ArrayList,也可以像以下那样强制转换:

Collection.shuffle((ArrayList)allAnswers);

List没有实现Collection接口,但是ArrayList执行:

https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html

答案 1 :(得分:0)

列出测验=新的ArrayList&lt;&gt;();

ArrayList<QuestionAndAnswer> quiz = new ArrayList<>();