Radio Group - 获取所选ID

时间:2014-05-06 16:36:41

标签: android radio-button

我正试图从收音机组中获取单选按钮的文本,但我失败了。

这是我收到的错误消息:

05-06 17:32:30.317  20460-20460/pt.smartgeo.aees E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: pt.smartgeo.aees, PID: 20460
    java.lang.NullPointerException
            at pt.smartgeo.aees.CreatePoint$1.onClick(CreatePoint.java:121)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            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:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

这是我的无线电组:

    <RadioGroup
            android:id="@+id/potencia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nColuna"
            android:layout_toRightOf="@+id/Potencia">

            <RadioButton
android:id="@+id/rb2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="100W" />

            <RadioButton
android:id="@+id/rb3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="150W" />

            <RadioButton
android:id="@+id/rb1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="250W" />

            <RadioButton
android:id="@+id/rb4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="400W" />

        </RadioGroup>

这就是我试图将其放入我的无线电组文本的地方:

RadioGroup group = (RadioGroup) view.findViewById(pt.smartgeo.aees.R.id.potencia);
int selectedId = group.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) view.findViewById(selectedId);
Toast.makeText(getActivity().getApplicationContext(),
    "rebenta? " + radioButton.getText() , Toast.LENGTH_SHORT).show();

我已经看到我的selectID不为空,并且我选择的每个单选按钮都不同。但我的祝酒词有java Null Pointer

我做过的一些记录和实际结果:

        Log.d("id do group", "Id do group: " + group.getId());
        Log.d("selected do group", "id do selected radio button " + group.getCheckedRadioButtonId());
        Log.d("selectedid", "selected id: " + selectedId);

结果:

05-07 10:26:15.374  13928-13928/pt.smartgeo.aees D/id do group﹕ Id do group: 2131296349
05-07 10:26:15.374  13928-13928/pt.smartgeo.aees D/selected do group﹕ id do selected radio button 2131296353
05-07 10:26:15.374  13928-13928/pt.smartgeo.aees D/selectedid﹕ selected id: 2131296353

4 个答案:

答案 0 :(得分:1)

RadioButtons没有android:id属性,但没有一个使用android:checked = "true"。不确定您是否在代码中检查了收音机。

答案 1 :(得分:1)

我终于找到了答案,那就是:

    final RadioGroup group = (RadioGroup) view.findViewById(pt.smartgeo.aees.R.id.potencia);
 int selectedId = group.getCheckedRadioButtonId();
                    RadioButton radio = (RadioButton) group.findViewById(selectedId);
                    Toast.makeText(getActivity().getApplicationContext(), "text: " + radio.getText().toString(), Toast.LENGTH_LONG).show();

答案 2 :(得分:0)

RadioButtons

没有android:id属性

答案 3 :(得分:0)

dd id to each

 <RadioGroup
        android:id="@+id/RB_options"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/RB_op1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:onClick="onRadioButtonClicked"
            android:text=""
            android:visibility="visible" />

        <RadioButton
            android:id="@+id/RB_op2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:onClick="onRadioButtonClicked"
            android:text=""
            android:visibility="visible" />

        <RadioButton
            android:id="@+id/RB_op3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:onClick="onRadioButtonClicked"
            android:text=""
            android:visibility="visible" />

        <RadioButton
            android:id="@+id/RB_op4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:onClick="onRadioButtonClicked"
            android:text=""
            android:visibility="visible" />
    </RadioGroup>

在java中

op1 = (RadioButton) findViewById(R.id.RB_op1);
        op2 = (RadioButton) findViewById(R.id.RB_op2);

        op3 = (RadioButton) findViewById(R.id.RB_op3);

        op4 = (RadioButton) findViewById(R.id.RB_op4);

        radioSelect = (RadioGroup) findViewById(R.id.RB_options);

这是onclick方法

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.RB_op1:
            if (checked)
                Toast.makeText(LearnersTest_activity.this,
                        op1.getText().toString(), Toast.LENGTH_LONG).show();
            break;
        case R.id.RB_op2:
            if (checked)
                Toast.makeText(LearnersTest_activity.this,
                        op2.getText().toString(), Toast.LENGTH_LONG).show();

            break;

        case R.id.RB_op3:
            if (checked)
                Toast.makeText(LearnersTest_activity.this,
                        op3.getText().toString(), Toast.LENGTH_LONG).show();

            break;

        case R.id.RB_op4:
            if (checked)
                Toast.makeText(LearnersTest_activity.this,
                        op4.getText().toString(), Toast.LENGTH_LONG).show();

            break;
        }
    }