gpt4 book ai didi

java - 单选按钮 If 语句

转载 作者:行者123 更新时间:2023-12-01 07:06:50 24 4
gpt4 key购买 nike

我正在检查单选按钮如何工作..在我的代码中,我使用2个单选按钮,当选择一个单选按钮时...我接受,另一个未选择..当我运行时,应用程序崩溃并在logcat中它说错误是空指针异常我在下面给出了我的代码..请检查

主要 Activity

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>

日志猫

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

最佳答案

试试这个代码

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;
}

}

关于java - 单选按钮 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22094700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com