英文:
Checking if RadioButtons are not checked in java
问题
我正在学习 Android,在 Java 中制作一个类似图片中所示的简单计算器。我的计算器
尝试运行应用时,两个单选按钮都没有被选中,因此当我按下“计算”按钮时,应用程序会关闭。
我尝试进行一个判断,如果两个按钮都没有被选中,应用程序会显示一个 Toast,但是应用程序无论如何都会关闭。
这是判断的代码(其中 btnSumar 和 btnRestar 是 RadioButtons):
if (!btnSumar.isChecked() && !btnRestar.isChecked()) {
Toast.makeText(MainActivity.this, "Please, select one option", Toast.LENGTH_SHORT).show();
}
这在我看来是合理的,但它不起作用。
谢谢您的帮助!
以下是 logcat 日志:
2020-09-06 18:51:48.814 10245-10245/com.triopsstudio.seccion01 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.triopsstudio.seccion01, PID: 10245
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:627)
at java.lang.Integer.parseInt(Integer.java:650)
at com.triopsstudio.seccion01.MainActivity.Calcular(MainActivity.java:53)
英文:
I'm learning android making a simple calculator in Java like the one in the image. my calculator
When I try the app, neither of the Radio Buttons are checked, so when I press the 'calculate' button the app shuts down.
I tried making a comprobation and if neither of the buttons are checked the app shows a Toast but the app shuts down anyway.
This is the comprobation code (where btnSumar and btnRestar are the RadioButtons):
if(!btnSumar.isChecked() && !btnRestar.isChecked()) {
Toast.makeText(MainActivity.this, "Please, select one option", Toast.LENGTH_SHORT).show();
}
It seems logic to me, but it doesn't work.
Thanks for helping!
this is logcat
2020-09-06 18:51:48.814 10245-10245/com.triopsstudio.seccion01 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.triopsstudio.seccion01, PID: 10245
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:7448) 
at android.view.View.performClickInternal(View.java:7425) 
at android.view.View.access$3600(View.java:810) 
at android.view.View$PerformClick.run(View.java:28305) 
at android.os.Handler.handleCallback(Handler.java:938) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:627)
at java.lang.Integer.parseInt(Integer.java:650)
at com.triopsstudio.seccion01.MainActivity.Calcular(MainActivity.java:53)
答案1
得分: 0
可以通过默认选中一个单选按钮,并获取其在 RadioGroup 中的 ID。
示例:
<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />
</RadioGroup>
MainActivity:
//顶层声明..
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
在 `onCreate(Bundle savedInstanceState)` 中:
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
// 从 radioGroup 获取选中的单选按钮
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// 根据返回的 ID 查找 radiobutton
radioSexButton = (RadioButton) findViewById(selectedId); // 这将得到选中的按钮的 ID
英文:
You can make one of radiobutton checked by default and it's id from radioGroup
ex:
<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />
</RadioGroup>
MainActivity:
//top level declaration..
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
Inside onCreate(Bundle savedInstanceState)
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);//this will have selected button id
答案2
得分: 0
使用isChecked()函数来检查每个单选按钮。
RadioButton maleRadioButton, femaleRadioButton;
maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButton);
femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButton);
然后根据代码情况使用结果来考虑检查单选按钮。
if (maleRadioButton.isChecked() || femaleRadioButton.isChecked()) {
Log.d("QAOD", "Gender is Selected");
} else {
Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
Log.d("QAOD", "Gender is Null");
}
英文:
Use the isChecked() function for every radioButton you have to check.
RadioButton maleRadioButton, femaleRadioButton;
maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButton);
femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButton);
Then use the result for your code case consideration to check radiobutton.
if (maleRadioButton.isChecked() || femaleRadioButton.isChecked()) {
Log.d("QAOD", "Gender is Selected");
} else {
Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
Log.d("QAOD", "Gender is Null");
}
答案3
得分: 0
根据您的堆栈跟踪,您遇到了NumberFormatException,因为您尚未输入任何内容。您尝试从空字符串中解析数字。Integer.parseInt("") 导致异常(MainActivity类的第53行)。
英文:
According to your stacktrace you have a NumberFormatException because you have no input yet. You try to parse number from empty string. Integer.parseInt("") leads to Exception ( lines 53 MainActivity class)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论