我正在檢查單選按鈕的工作方式..在我的代碼中,我使用了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
的方法的ID findViewByID是不正確的。 –
哪一個..你可以請粘貼特定的線..我沒有使用單選按鈕多數民衆贊成 – Jocheved
這一個:'button1 =(RadioButton)findViewById(選擇);'因此單選按鈕沒有正確實例化,因此空指針豁免 –