-3

I am implementing radio group. When i try to run the app, i get this error java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference.

radio_group_your_choice = (RadioGroup) findViewById(R.id.radio_group_your_choice);
        male_radioButton = (RadioButton) findViewById(R.id.male_radioButton);
        female_radioButton = (RadioButton) findViewById(R.id.female_radioButton);

        radio_group_your_choice.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                gender = radioButton.getText().toString();
            }
        });

<RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="0dp"
                android:layout_marginBottom="15dp">

                <RadioGroup
                    android:id="@+id/radio_group_your_choices"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <RadioButton
                        android:id="@+id/delivery_radioButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Pick Up"
                        android:textColor="@color/dark_grey"/>

                    <RadioButton
                        android:id="@+id/pickup_radioButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginStart="20dp"
                        android:text="Delivery"
                        android:textColor="@color/dark_grey"/>


                </RadioGroup>

            </RelativeLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jacky
  • 298
  • 3
  • 15

1 Answers1

0

this radio group is null:

radio_group_your_choice

try to ask before setOnCheckedChangeListener

if (radio_group_your_choice!= null)

to avoid this exeption.

batsheva
  • 2,175
  • 1
  • 20
  • 32
  • Still the same error java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference – Jacky Jul 30 '17 at 06:34
  • radioGroup, radioButtons are null. – Jacky Jul 30 '17 at 06:35
  • try to check why is it null, do you have it in your screen, or it is in another layout? it seems this radiobutton didn't create – batsheva Jul 30 '17 at 06:36
  • yes but did you load in OnCreate this layout or another one? how dose this layout call? and waht did you put in setContentView? – batsheva Jul 30 '17 at 06:40
  • https://paste.ofcode.org/txjFQ3w9DRCqmpyjRamc7X Here i have added the code. I have set activity_search – Jacky Jul 30 '17 at 06:43
  • setContentView(R.layout.activity_search); This is what has been set before. But I am working on fragment_main.xml – Jacky Jul 30 '17 at 06:53