2016-03-31 36 views
0

我遇到了我的radiogroup問題,如果用戶不會選擇答案,應用程序將強制關閉,而且,如果用戶選擇答案,則第二個答案會相同題。另外,當用戶在按下下一個按鈕時沒有選擇任何答案時,如何添加提示消息?您的回答將不勝感激。謝謝RadioButton force關閉應用程序

這裏是Java文件

public class QuizActivity extends Activity{ 
     List<Question> quesList; 
     int score=0; 
     int qid=0; 
     Question currentQ; 
     TextView txtQuestion; 
     RadioButton rda, rdb, rdc; 
     Button butNext; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_quiz); 
      DbHelper db=new DbHelper(this); 
      quesList=db.getAllQuestions(); 
      currentQ=quesList.get(qid); 
      txtQuestion=(TextView)findViewById(R.id.textView1); 
      rda=(RadioButton)findViewById(R.id.radio0); 
      rdb=(RadioButton)findViewById(R.id.radio1); 
      rdc=(RadioButton)findViewById(R.id.radio2); 
      butNext=(Button)findViewById(R.id.button1); 
      setQuestionView(); 
      butNext.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 



        RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1); 
        RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId()); 
        Log.d("yourans", currentQ.getANSWER()+" "+answer.getText()); 
        if(currentQ.getANSWER().equals(answer.getText())) 
        { 
         score++; 
         Log.d("score", "Your score"+score); 
        } 
        if(qid<5){ 
         currentQ=quesList.get(qid); 
         setQuestionView(); 
        }else{ 
         Intent intent = new Intent(QuizActivity.this, ResultActivity.class); 
         Bundle b = new Bundle(); 
         b.putInt("score", score); //Your score 
         intent.putExtras(b); //Put your score to your next Intent 
         startActivity(intent); 
         finish(); 
        } 
       } 
      }); 
     } 
     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.activity_quiz, menu); 
      return true; 
     } 
     private void setQuestionView() 
     { 
      txtQuestion.setText(currentQ.getQUESTION()); 
      rda.setText(currentQ.getOPTA()); 
      rdb.setText(currentQ.getOPTB()); 
      rdc.setText(currentQ.getOPTC()); 
      qid++; 
     } 
    } 

這裏是XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".QuizActivity"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical" 
     android:layout_gravity="center"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Large Text" 
      android:textAppearance="?android:attr/textAppearanceLarge"/> 

     <RadioGroup 
      android:id="@+id/radioGroup1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.04"> 
      <RadioButton 
       android:id="@+id/radio0" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:checked="false" 
       android:text="RadioButton" /> 
      <RadioButton 
       android:id="@+id/radio1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:checked="false" 
       android:text="RadioButton" /> 
      <RadioButton 
       android:id="@+id/radio2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:checked="false" 
       android:text="RadioButton" /> 
     </RadioGroup> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" /> 

     <Button 
       android:id="@+id/button1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/str_next" 
       android:visibility="visible"/> 
    </LinearLayout> 
</RelativeLayout> 
+0

您可以在'next's'區域的相應邏輯中添加一個'Toast'。 – andre3wap

回答

0

如果用戶不選擇任何你可以控制它是這樣的:

if(grp.getCheckedRadioButtonId() != null){ 
    //do something 
}else{ 
    Toast.makeText(getActivity(), "You have to choose one option", Toast.LENGTH_SHORT).show(); 
} 

關於下一個問題的相同答案,您必須在所有單選按鈕之前重置。例如:

rda.setChecked(false); 
rdb.setChecked(false); 
rdc.setChecked(false); 
+0

感謝您的回覆。但是,行if(grp.getCheckedRadioButtonId()!= null)在紅色線條下,說operator!=不能應用於int,null – Dreamer

+0

好吧,所以試試這個:if(grp.getCheckedRadioButtonId()> 0) – ARP

+0

另外, getActivity在紅線下。說不能解決方法 – Dreamer