【问题标题】:RadioButton - Checked if radiobutton is checked inside a radiogroup - Android单选按钮 - 检查单选按钮是否在单选组内被选中 - Android
【发布时间】:2016-01-28 14:20:02
【问题描述】:

我在检查 radiobutton 中是否在我的 radiogroup 中选择了一个时遇到问题,我尝试了很多方法,例如 if(rg.getCheckedRadioButtonId() == -1),但仍然无法处理我的代码。

在我的日志猫String selectedansText = selectedans.getText().toString(); 在这一行中,我的 logcat 指出了问题,有人可以帮助我吗?

我也尝试了.isChecked,但它不起作用。

bt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

         RadioButton selectedans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
         String selectedansText = selectedans.getText().toString();

         if (rg.getCheckedRadioButtonId()== -1){
             Toast.makeText(grade_four_post_test.this, 
                           "choose answer", 
                           Toast.LENGTH_LONG).show();
         } else { //do something }

PS:bt 声明正确

01-28 22:27:07.390 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at android.view.View.performClick(View.java:4463)
01-28 22:27:07.391 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at android.view.View$PerformClick.run(View.java:18770)
01-28 22:27:07.391 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at android.os.Handler.handleCallback(Handler.java:808)
01-28 22:27:07.391 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:103)
01-28 22:27:07.392 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at android.os.Looper.loop(Looper.java:193)
01-28 22:27:07.392 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5292)
01-28 22:27:07.392 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
01-28 22:27:07.393 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
01-28 22:27:07.393 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
01-28 22:27:07.393 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
01-28 22:27:07.394 27612-27612/com.example.computer.mathkiddofinal:second W/System.err:     at dalvik.system.NativeStart.main(Native Method)
01-28 22:27:07.394 27612-27612/com.example.computer.mathkiddofinal:second W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
01-28 22:27:07.396 27612-27612/com.example.computer.mathkiddofinal:second E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                            Process: com.example.computer.mathkiddofinal:second, PID: 27612
                                                                                            java.lang.NullPointerException
                                                                                                at com.example.computer.mathkiddofinal.grade_level.post_test.grade_four_post_test$1.onClick(grade_four_post_test.java:117)
                                                                                                at android.view.View.performClick(View.java:4463)
                                                                                                at android.view.View$PerformClick.run(View.java:18770)
                                                                                                at android.os.Handler.handleCallback(Handler.java:808)
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                                                at android.os.Looper.loop(Looper.java:193)
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                                at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                                                                at dalvik.system.NativeStart.main(Native Method)
01-28 22:27:07.400 696-9034/system_process V/Provider/Settings:  from settings cache , name = dropbox:data_app_crash , value = null
01-28 22:27:07.401 696-9034/system_process D/dalvikvm: create interp thread : stack size=128KB
01-28 22:27:07.401 696-9034/system_process D/dalvikvm: create new thread
01-28 22:27:07.401 696-9034/system_process D/dalvikvm: new thread created
01-28 22:27:07.401 696-9034/system_process D/dalvikvm: update thread list
01-28 22:27:07.402 696-28496/system_process D/dalvikvm: threadid=79: interp stack at 0x63b53000
01-28 22:27:07.402 696-28496/system_process D/dalvikvm: threadid=79: created from interp
01-28 22:27:07.402 696-9034/system_process D/dalvikvm: start new thread
01-28 22:27:07.402 696-9034/system_process V/Provider/Settings:  from settings cache , name = send_action_app_error , value = 1
01-28 22:27:07.402 696-28496/system_process D/dalvikvm: threadid=79: notify debugger
01-28 22:27:07.403 696-9034/system_process W/ActivityManager:   Force finishing activity com.example.computer.mathkiddofinal/.grade_level.post_test.grade_four_post_test

【问题讨论】:

  • logcat 指向哪里?您应该发布 logcat 输出....

标签: android radio-button


【解决方案1】:

您将为此获得 NullPointerException:

  RadioButton selectedans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
         String selectedansText = selectedans.getText().toString();//NullPointer

因为如果没有选择RadioButtonfindViewById 将不起作用,所以你的RadioButton 没有被初始化。你应该这样做:

 int radioButtonId = rg.getCheckedRadioButtonId();

if(radioButtonId!=-1){

   //do Your stuff
    RadioButton selectedans = (RadioButton) findViewById(radioButtonId);
    String selectedansText = selectedans.getText().toString();

}else{
 Toast.makeText(grade_four_post_test.this, 
                           "choose answer", 
                           Toast.LENGTH_LONG).show();
}

【讨论】:

  • 它工作!谢谢你:D。我标记了这个正确的完整细节,
【解决方案2】:
    @Override
    public void onClick(View v) {

            // get selected radio button from radioGroup
        int selectedId = radioSexGroup.getCheckedRadioButtonId();

        // find the radiobutton by returned id
            radioSexButton = (RadioButton) findViewById(selectedId);

        Toast.makeText(MyAndroidAppActivity.this,
            radioSexButton.getText(), Toast.LENGTH_SHORT).show();

      //Start your work here

    }

});

【讨论】:

  • 这对问题代码没有影响,也不回答问题...
【解决方案3】:
if(rg.getCheckedRadioButtonId()== -1) this means no radio button is selected.

在其他部分添加以下行

int selectedId = rg.getCheckedRadioButtonId();
RadioButton selectedans = (RadioButton) findViewById(selectedId);
Toast.makeText(grade_four_post_test.this,selectedans.getText(),Toast.LENGTH_SHORT).show();

【讨论】:

  • 并删除 RadioButton selectedans = (RadioButton) findViewById(rg.getCheckedRadioButtonId()); String selectedansText = selectedans.getText().toString();行
  • 我无法删除该行,因为我正在获取在我的 radiogroup 上选择的单选按钮的文本值并与另一个字符串变量进行比较。
  • in else 你可以添加那些行
猜你喜欢
  • 2019-12-21
  • 2014-09-19
  • 1970-01-01
  • 2014-02-06
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
相关资源
最近更新 更多