I do have a fragment inflating a layout that contains a single button.I added radiogroup and radiobuttons dynamically in onCreateView() method.But the problem is in onclicklistener of that button. While retrieving for radiobutton text that's checked in a radioGroup I do get nulpointerexception.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View v =  inflater.inflate(R.layout.variant_choose,null);
    Button button = (Button) v.findViewById(R.id.apply_variant_button);
    LinearLayout ll = (LinearLayout) v.findViewById(R.id.layout_for_radiobuttons_in_variants);
    final RadioGroup rg = new RadioGroup();
    rg.setId(View.generateViewId());
    rg.setOrientation(LinearLayout.HORIZONTAL);
    RadioButton rb[] = new RadioButton[5];
    for(int j=0;j<5;j++){
            rb[j] = new RadioButton(getActivity());
            rb[j].setId(View.generateViewId());
            rb[j].setText(j);
            rg.addView(rb[j]);       
    }
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                RadioButton rb1 = (RadioButton) v.findViewById(rg.getCheckedRadioButtonId());
                String str1 = (String) rb1.getText();
        }
    });
  return v;
}
View v contains dynamically created radiogroup and button till onCreateView. But later in onclicklistener it's not the case.It only contains button that's defined in xml file? What could be the reason for this? How can I get the text of checked radiobutton?
 
     
    