Search code examples
androidnullpointerexceptiononclicklistenerandroid-dialogfragmentandroid-radiobutton

DialogFragment RadioGroup onClick NullPointerException


I'm getting an NPE on my code. I'm trying to get the value of the selected RadioButton. When I click the Button the Exception gets fired, don't know why, I could imagine that it takes the wrong view...

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class CategoryDialog extends DialogFragment{

Button btnDone;
RadioGroup rg;
RadioButton radioBtn;
String selectedCategory;
Communicator communicator;

public static interface Communicator{
    public void onCategorySelected(String category);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    communicator = (Communicator) activity;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_video_detail_category_dialog, container);
    this.getDialog().setTitle(getString(R.string.z2_videodetail_category_dialog_title));
    btnDone = (Button) view.findViewById(R.id.category_done_btn);
    rg = (RadioGroup) view.findViewById(R.id.category_radioGroup);
    btnDone.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int selectedId = rg.getCheckedRadioButtonId();
            radioBtn = (RadioButton) v.findViewById(selectedId);
            selectedCategory = radioBtn.getText().toString();
            if(v.getId() == R.id.category_done_btn){
                communicator.onCategorySelected(selectedCategory);
                dismiss();
            }
        }
    });
    return view;
  }

}

XML-FILE

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<RadioGroup
    android:id="@+id/category_radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp" >

    <RadioButton 
        android:id="@+id/category_radioGroup_radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/z2_videodetail_category_dialog_radioButton1" />

    <RadioButton 
        android:id="@+id/category_radioGroup_radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/z2_videodetail_category_dialog_radioButton2" />

    <RadioButton 
        android:id="@+id/category_radioGroup_radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/z2_videodetail_category_dialog_radioButton3" />

    <RadioButton 
        android:id="@+id/category_radioGroup_radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/z2_videodetail_category_dialog_radioButton4" />   
</RadioGroup>

<Button
    android:id="@+id/category_done_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/category_radioGroup"
    android:text="@string/z2_videodetail_category_dialog_done_btn" />

</RelativeLayout>

Thank you very much, highly appreciating your help! :)


Solution

  • You named your fragment View as view, but you also have the variable view inside of the onClick method referring to the button itself, so when you are finding the radio button by id, the program is looking into the wrong view. Is looking inside the button instead of looking into your fragment. Rename the variable view on the onClick method to avoid this confusion

    And you have to declare the view object as final so it's available in the anonymous inner class!

        final View view = inflater.inflate(R.layout.fragment_video_detail_category_dialog, container);
        this.getDialog().setTitle(getString(R.string.z2_videodetail_category_dialog_title));
        final RadioGroup rg = (RadioGroup) view.findViewById(R.id.category_radioGroup);
    
        @Override
        public void onClick(View v) {    //Change it from view to v or whatever you want
            int selectedId = rg.getCheckedRadioButtonId();
            radioBtn = (RadioButton) view.findViewById(selectedId);
            selectedCategory = radioBtn.getText().toString();
            if(v.getId() == R.id.category_done_btn){ //And update this value
                communicator.onCategorySelected(selectedCategory);
                dismiss();
            }
        }