Question

I am getting following exception in my shared preference:-

    04-24 15:33:27.030: E/AndroidRuntime(23540): FATAL EXCEPTION: main
04-24 15:33:27.030: E/AndroidRuntime(23540): java.lang.NullPointerException
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:384)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:379)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at com.ht.webpageloadingapplication.ListViewSettingsActivity.loadCheckBoxSavedPreferences(ListViewSettingsActivity.java:89)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at com.ht.webpageloadingapplication.CustomAdapterSettings.getView(CustomAdapterSettings.java:185)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.AbsListView.obtainView(AbsListView.java:1469)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.ListView.makeAndAddView(ListView.java:1789)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.ListView.fillDown(ListView.java:708)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.ListView.fillFromTop(ListView.java:765)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.ListView.layoutChildren(ListView.java:1642)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.AbsListView.onLayout(AbsListView.java:1299)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.view.View.layout(View.java:7225)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:943)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.view.View.layout(View.java:7225)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.view.View.layout(View.java:7225)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1290)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1166)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1083)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.view.View.layout(View.java:7225)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.widget.FrameLayout.onLayout(FrameLayout.java:369)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.view.View.layout(View.java:7225)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1181)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1901)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.os.Handler.dispatchMessage(Handler.java:130)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.os.Looper.loop(SourceFile:351)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at android.app.ActivityThread.main(ActivityThread.java:3820)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at java.lang.reflect.Method.invoke(Method.java:538)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659)
04-24 15:33:27.030: E/AndroidRuntime(23540):    at dalvik.system.NativeStart.main(Native Method)

And This is my function inside an activity for getting shared preference data:-

public boolean loadCheckBoxSavedPreferences(String key, int position) {

        SharedPreferences sharedPrefCheckBox = PreferenceManager
                .getDefaultSharedPreferences(this);
        if(position == 4 || position == 5 || position == 6 || position == 11 || position == 13 || position == 15 || position == 18 || position == 20)
        return sharedPrefCheckBox.getBoolean(key, true);
        else
            return sharedPrefCheckBox.getBoolean(key, false);
    }

//It shows error on very first line of shared preferece function. This is the line causing exception:-
SharedPreferences sharedPrefCheckBox = PreferenceManager
                    .getDefaultSharedPreferences(this);

I call this function from adapter class. I have debugged my code and everything looks fine.

Was it helpful?

Solution

If you use this code:

SharedPreferences sharedPrefCheckBox = PreferenceManager
                .getDefaultSharedPreferences(this);

inside an adapter class, this means the adapter class instance, not a class that inherits from Context.

You need to change that to use your activity, e.g. by passing a reference to the activity to your adapter class (context is usually passed in the adapter constructor). You can then create a variable on the class for the context. In the constructor, set the context passed as parameter to this variable. Use this variable when getting shared preferences.

Note: some adapter classes have getContext() method that you can use.

OTHER TIPS

Don't use getBaseContext

Use yourAcivity.this


Try this:

SharedPreferences settings  = getSharedPreferences("config", 0);
Boolean yourvariable        = settings.getBoolean(key, false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top