Question

I am trying to get the text of a radio button from a radio group but I am failing.

This is the error message that I have got:

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)

This is my radiogroup:

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

And this is where I'm trying to get it my radiogroup text:

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();

I already saw that my selectID isn't null and it's different for each radio button I choose. But my toast like has java Null Pointer

some logcats I did and actual results:

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

results:

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
Was it helpful?

Solution 2

I finally found my answer and that is:

    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();

OTHER TIPS

You have no android:id attribute for RadioButtons and none of them uses android:checked = "true" . Not sure if you have checked your radio's in code.

You have no android:id attribute for RadioButtons

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>

in 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);

this is the onclick methode

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;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top