2014-02-28 100 views
0

我正在檢查單選按鈕的工作方式..在我的代碼中,我使用了2個單選按鈕,並且選擇了一個...因爲我接受而另一個未選中。當我運行應用程序時崩潰在logcat中跟它的錯誤是空指針異常 和我給我下面的代碼..請RadioButton如果語句

MainActiivyt

public class MainActivity extends Activity { 

    RadioGroup group1; 
    RadioButton button1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     group1 = (RadioGroup) findViewById(R.id.rg1); 

     int selected = group1.getCheckedRadioButtonId(); 
     button1 = (RadioButton) findViewById(selected); 

     if ("I Accept".equals(button1.getText().toString())) { 
      Toast.makeText(this,"Correct!",Toast.LENGTH_SHORT).show(); 
     } else { 
      Toast.makeText(this,"Incorrect.",Toast.LENGTH_SHORT).show(); 
     } 

    } 
} 

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=".MainActivity" >  

    <RadioGroup 
     android:id="@+id/rg1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <RadioButton 
      android:id="@+id/radioButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="onRadioButtonClicked" 
      android:text="I Accept" /> 

    <RadioButton 
     android:id="@+id/radioButton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="19dp" 
     android:text="Not Accept" 
     android:onClick="onRadioButtonClicked" /> 

    </RadioGroup> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="124dp" 
     android:layout_toRightOf="@+id/rg1" 
     android:text="Button" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_below="@+id/rg1" 
     android:layout_marginTop="172dp" 
     android:text="TextView" /> 

</RelativeLayout> 

logcat的

02-28 11:57:20.835: E/AndroidRuntime(6713): FATAL EXCEPTION: main 
02-28 11:57:20.835: E/AndroidRuntime(6713): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radiobutton/com.example.radiobutton.MainActivity}: java.lang.NullPointerException 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at android.os.Looper.loop(Looper.java:137) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at dalvik.system.NativeStart.main(Native Method) 
02-28 11:57:20.835: E/AndroidRuntime(6713): Caused by: java.lang.NullPointerException 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at com.example.radiobutton.MainActivity.onCreate(MainActivity.java:25) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at android.app.Activity.performCreate(Activity.java:5104) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
02-28 11:57:20.835: E/AndroidRuntime(6713):  ... 11 more 
+1

的方法的ID findViewByID是不正確的。 –

+0

哪一個..你可以請粘貼特定的線..我沒有使用單選按鈕多數民衆贊成 – Jocheved

+1

這一個:'button1 =(RadioButton)findViewById(選擇);'因此單選按鈕沒有正確實例化,因此空指針豁免 –

回答

2

更換此單選組嘗試這個代碼

public void onRadioButtonClicked(View view) { 
// Is the button now checked? 
boolean checked = ((RadioButton) view).isChecked(); 

// Check which radio button was clicked 
switch(view.getId()) { 
    case R.id.radioButton1: 
     if (checked) 
      // RadioButton id 1 
     break; 
    case R.id.radioButton2: 
     if (checked) 
      //RadioButton id 2 
     break; 
} 

}

+0

謝謝Rishivedi ...這是我想要的...... – Jocheved

1

此:

button1 = (RadioButton) findViewById(selected); 

應該是:

button1 = (RadioButton) findViewById(R.id.radioButton1); 

小姐,你當單選按鈕被選中的聽衆......參考Android開發者page

+0

感謝讓我檢查 – Jocheved

2

您在onCreate上沒有您的RadioButtons,因此selected是-1,因此您使用findViewById找到了button1的ID。

添加android:checked="true"RadioButton之一,在XML或更改使用其ID

button1 = (RadioButton) findViewById(selected); 

button1 = (RadioButton) findViewById(R.id.radioButton1); 
+0

感謝讓我檢查 – Jocheved

+0

不客氣。如果有幫助,你可以接受答案。 – Apoorv

1

聲明你的單選按鈕;

RadioButton button1 = (RadioButton)findViewById(R.id.radioButton1); 
+0

RadioButton button1已經定義在頂部。所以,你最好改爲按鈕1 = ... –

+0

我剛剛向他展示瞭如何使用xml中的id來聲明按鈕。 – Lokesh

2

從你的XML

<RadioGroup 
     android:id="@+id/rg1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <RadioButton 
      android:id="@+id/radioButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="true" 
      android:onClick="onRadioButtonClicked" 
      android:text="I Accept" /> 

     <RadioButton 
      android:id="@+id/radioButton2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="19dp" 
      android:onClick="onRadioButtonClicked" 
      android:text="Not Accept" /> 
    </RadioGroup>