0

I am trying to load some SharedPreferences data (checkbox state) from a dialog. I have the following code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.proficiency_dialog, null);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        updateProficiency = (Button) view.findViewById(R.id.update_proficiency);
        updateProficiency.setOnClickListener(this);
        cancelProficiency = (Button) view.findViewById(R.id.cancel_proficiency);
        cancelProficiency.setOnClickListener(this);
        STR_Prof_checkbox = (CheckBox) getDialog().findViewById(R.id.STR_save_checkbox);
        loadSavedPreferences();
        setCancelable(false);
        return view;
    }

    public void loadSavedPreferences() {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(getContext());
        boolean STRProfCheckBoxValue = sharedPreferences.getBoolean("STR_Save_CheckBox_Value", false);
        if (STRProfCheckBoxValue) {
            STR_Prof_checkbox.setChecked(true);
        } else {
            STR_Prof_checkbox.setChecked(false);
        }
    }

However, I receive a null pointer exception on the loadSavedPreferences() method. What am I missing that's causing the error?

LOGCAT:

01-18 01:51:55.273 26730-26730/com.oozhangem.dd5echaracter E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.oozhangem.dd5echaracter, PID: 26730
 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setChecked(boolean)' on a null object reference
     at com.oozhangem.dd5echaracter.ProficiencyDialog.loadSavedPreferences(ProficiencyDialog.java:47)
     at com.oozhangem.dd5echaracter.ProficiencyDialog.onCreateView(ProficiencyDialog.java:35)
     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5294)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
6
  • 1
    Please post your logcat error output.
    – Rohit5k2
    Jan 17, 2016 at 14:40
  • Yes, logcat will be usefull. But i think getContext() is null. You can get Context from container. And off top: don't use inflater with null. Use conteiner object as parameter.
    – Puzirki
    Jan 17, 2016 at 14:44
  • Added logcat, @Puzirki and could you explain using container objects as parameters? Jan 17, 2016 at 14:58
  • He is saying make loadSharedPreferences accept a Context parameter, and call it with loadSharedPreferences(container.getContext()); That shouldn't fix this issue, but it's a good practice regardless. Jan 17, 2016 at 14:59
  • Michael Zhang, Take a look this thread about it: stackoverflow.com/questions/24832497/… And: possiblemobile.com/2013/05/layout-inflation-as-intended
    – Puzirki
    Jan 17, 2016 at 15:00

1 Answer 1

0

Replace

SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(getContext());

With

SharedPreferences preferences = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);

Or

SharedPreferences pref = getActivity().getPreferences(0);

Also replace reference of STR_Prof_checkbox with below.

STR_Prof_checkbox = (CheckBox) view.findViewById(R.id.STR_save_checkbox);
4
  • I have other activities which use SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(getContext()); So I would imagine that this is not the issue? Jan 17, 2016 at 15:11
  • onCreateView is a method of fragment not of Activity. Jan 17, 2016 at 15:16
  • My mistake - i meant fragment. I am using fragments for swipe views. Jan 17, 2016 at 15:17
  • getContext() returns activity context not fragment context Jan 17, 2016 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.