Search code examples
androidandroid-sharedpreferences

Cannot save RadioGroup's selected Item in sharedPreferences - Fragment


I have fragment and inside fragment, i want to store selected choice of RadioGroup (Male/Female) in SharedPreferences. Following code i have tried, it works if i use it in Activity, but its not working in Fragment.

while loading and inserting int value of selected choice, it returns correct value - Male = 0, Female = 1, but during loading data, it crashes with NullPointerException on following statement

 if (i >= 0) 
         ((RadioButton) ((RadioGroup) getActivity().findViewById(R.id.genderSelect)).getChildAt(i)).setChecked(true);

I have tried searching lots of solution on stackoverflow, but no luck. any suggestion or help on this? Thank you.

 public class MyFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

     View rootView = inflater.inflate(R.layout.profile_fragment,container,false);

    //Initializing views
    saveButton = (Button) rootView.findViewById(R.id.buttonSave);
    cancelButton = (Button) rootView.findViewById(R.id.buttonCancel);

    userGender = (RadioGroup) rootView.findViewById(R.id.genderSelect);

    loadProfileFields();
   return rootView;
   }


 private void saveProfile() {

    savedFileData = getActivity().getSharedPreferences(SHARED_PREF_FILE_NAME,Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = savedFileData.edit();
 RadioGroup localRadioGroup = (RadioGroup) getActivity().findViewById(R.id.genderSelect);
    editor.putInt(preference_key_profile_gender,
             localRadioGroup.indexOfChild(getActivity().findViewById(localRadioGroup.getCheckedRadioButtonId())));
  editor.apply();
 }


 private void loadProfileFields() {

    SharedPreferences loadProfile = getActivity().getSharedPreferences(SHARED_PREF_FILE_NAME, Context.MODE_PRIVATE);
    if(loadProfile != null){

        int i = loadProfile.getInt(preference_key_profile_gender, -1);
       // i gives me the right value, but it throws NullPointerExc inside If statement
        if( i >= 0){

                ((RadioButton) ((RadioGroup) getActivity().findViewById(R.id.genderSelect)).getChildAt(i)).setChecked(true);


        }

    }
}

Solution

  • You don't need to findViewById() everytime you want to use a view. Change your methods -

    private void saveProfile() {
    
        savedFileData = getActivity().getSharedPreferences(SHARED_PREF_FILE_NAME,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = savedFileData.edit();
    
        editor.putInt(preference_key_profile_gender,
                 userGender.indexOfChild(userGender.getCheckedRadioButtonId());
      editor.apply();
     }
    
    
     private void loadProfileFields() {
    
        SharedPreferences loadProfile = getActivity().getSharedPreferences(SHARED_PREF_FILE_NAME, Context.MODE_PRIVATE);
        if(loadProfile != null){
    
            int i = loadProfile.getInt(preference_key_profile_gender, -1);
           // i gives me the right value, but it throws NullPointerExc inside If statement
            if( i >= 0){
    
                    ((RadioButton)userGender.getChildAt(i)).setChecked(true);
    
    
            }
    
        }
    }