2016-04-29 42 views
0

問候窺視,需要有關此錯誤的幫助。嘗試使用谷歌搜索,但沒有解決問題。我確實發表了錯誤發生在哪一行的評論。下面的代碼嘗試在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(); 
    } 
} 
+1

嘗試改變'公開名單 allAnswers;''到公衆的ArrayList allAnswers;' –

+0

我先試試這個方法,但沒有奏效。 –

回答

0

列表測驗=新的ArrayList <>();

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

你可以給你的答案添加一些補充嗎? –

+0

我的意思是改變你的List <> quiz = new ArrayList <>();到我之前提供的代碼 – Gee

相關問題