2015-11-05 27 views
0

我已經動態地創建了2個單選按鈕組。每個單選按鈕代表一個問題的答案。我setOnCheckedChangeListener查看哪些漫遊者是正確的。所以,當我按下一個按鈕時,我想擁有所有電臺組clearCheck()以及正確答案setTextColor(Color.GREEN)。有了這段代碼,當我使用radioGroup[i].clearCheck();時,出現此錯誤:Attempt to invoke virtual method 'java.lang.Object android.widget.RadioButton.getTag()' on a null object reference。我怎樣才能解決這個問題?我怎樣才能設置文本顏色綠色,只有正確的答案?這裏是我的代碼:如何清除所有無線電組和setTextColor到Java中的一個單選按鈕?

 radioGroup[i].setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId); 
       int CorrectAnswer = Integer.parseInt(checkedRadioButton.getTag().toString()); 
       switch (checkedId) { 
        case 0: 
         if (CorrectAnswer == 1) { 
          Toast.makeText(getApplicationContext(), "You clicked the correct answer ", Toast.LENGTH_SHORT).show(); 
          score++; 
         } else { 
          Toast.makeText(getApplicationContext(), "You clicked the incorrect answer ", Toast.LENGTH_SHORT).show(); 
         } 
         break; 
        case 1: 
         if (CorrectAnswer == 1) { 
          Toast.makeText(getApplicationContext(), "You clicked the correct answer ", Toast.LENGTH_SHORT).show(); 
          score++; 
         } else { 
          Toast.makeText(getApplicationContext(), "You clicked the incorrect answer ", Toast.LENGTH_SHORT).show(); 
         } 
         break; 
       } 
      } 
     }); 

     resetButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       for (int i = 0; i < radioGroup.length; i++) { 
        radioGroup[i].clearCheck(); // doesn't work 
        for (int j = 0; j < answer.length; j++) { 
         radioGroup[i].getChildAt(j).setBackgroundColor(Color.WHITE); 
         ((RadioButton) radioGroup[i].getChildAt(j)).setTextColor(Color.GREEN); 
        } 
       } 
      } 
     }); 

謝謝!

+0

請顯示代碼,其中在'radioGroup中[I]' –

+0

'radioGroup中加入視圖=新的RadioGroup [2]; answer = new RadioButton [2]; int i = 0; 爲(問題QN:題){ radioGroup中[I] =新RadioGroup中(本); int j = 0; 爲(接聽:回答){ 如果(qn.getID()== an.getQuestion_id_answer()){ 答案[J] =新單選按鈕(在此); answer [j] .setText(an.getAnswer()); answer [j] .setId(j); answer [j] .setTag(String.valueOf(an.getCorrect_answer())); radioGroup [i] .addView(answer [j]); j ++; } } linearLayout.addView(radioGroup中[1]); }' –

+0

爲了看得更清楚:[鏈接](http://gogo.ro/6.png) –

回答

0

大概onCheckedChanged方法正在調用時clearCheck方法被調用以便補充null檢查在onCheckedChanged呼籲setTag方法toString()方法之前:

@Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
     RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId); 
     if(checkedRadioButton!=null) 
     if(checkedRadioButton.getTag() !=null){ 
     int CorrectAnswer = Integer.parseInt(
         checkedRadioButton.getTag().toString()); 
      ....your code here 

     } 
    } 
+0

好的,但我該怎麼做? –

+0

@AlexM .:我知道。看到我的編輯答案 –

+0

沒有工作。現在看來'onCheckedChanged'不起作用。錯誤是:'java.lang.ClassCastException:android.widget.RadioGroup不能轉換爲android.widget.RadioButton'。 –

相關問題