检索所选RADIO按钮的值时出错

时间:2015-05-12 07:30:20

标签: android

我试图在点击SUBMIT按钮时获取所有选定单选按钮的值,但我在行处获得NULLPOINTER异常:

database/migrations/xxx.go

错误:

String selection = (String) btn.getText();

提交按钮代码:

05-12 12:54:52.261: E/AndroidRuntime(14113): FATAL EXCEPTION: main
05-12 12:54:52.261: E/AndroidRuntime(14113): Process: com.androidhub4you.multilevellistview, PID: 14113
05-12 12:54:52.261: E/AndroidRuntime(14113): java.lang.NullPointerException
05-12 12:54:52.261: E/AndroidRuntime(14113):    at com.androidhub4you.multilevellistview.MainActivity$5.onClick(MainActivity.java:249)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at android.view.View.performClick(View.java:4456)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at android.view.View$PerformClick.run(View.java:18465)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at android.os.Handler.handleCallback(Handler.java:733)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at android.os.Looper.loop(Looper.java:136)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at android.app.ActivityThread.main(ActivityThread.java:5086)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at java.lang.reflect.Method.invokeNative(Native Method)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at java.lang.reflect.Method.invoke(Method.java:515)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-12 12:54:52.261: E/AndroidRuntime(14113):    at dalvik.system.NativeStart.main(Native Method)

完整代码:

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

                if(radioGroup.getCheckedRadioButtonId()!=-1){
                    String selection = btn.getText().toString();
                    Toast.makeText(getApplicationContext(), "You are submitting : " + selection, Toast.LENGTH_LONG).show();
                }

            }
        });

4 个答案:

答案 0 :(得分:0)

现在试试这个:      selection = btn.getText()。toString();

答案 1 :(得分:0)

 private RadioButton btn;




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

             if(radioGroup.getCheckedRadioButtonId()!=-1){
                int id= radioGroup.getCheckedRadioButtonId();
                btn = (RadioButton)radioGroup.getChildAt(id);
                String selection = btn.getText().toString();
                Toast.makeText(getApplicationContext(), "You are submitting : " + selection, Toast.LENGTH_LONG).show();
             }


        }
    });

答案 2 :(得分:0)

我认为你在这里做了太多尴尬的事情。你只需要转换单选按钮并获得这样的字符串。

 if(radioGroup.getCheckedRadioButtonId()!=-1){
                        int id= radioGroup.getCheckedRadioButtonId();
                        RadioButton btn = (RadioButton)radioGroup.findViewById(id);                       
                        String selection = btn.getText().toString();
                        Toast.makeText(getApplicationContext(), "You are submitting : " + selection, Toast.LENGTH_LONG).show();
                     }

不要将字符串加倍String selection = (String) btn.getText().toString()......这是错误的方法......

答案 3 :(得分:0)

尝试在没有 RadioGroup 的情况下调用 findViewById()

if(radioGroup.getCheckedRadioButtonId()!=-1) {
    int ID = radioGroup.getCheckedRadioButtonId();
    RadioButton rbSelection = (RadioButton) findViewById(id);
    String selection = rbSelection.getText().toString();
}

或更简单地说,

String selection = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId())).getText().toString();