I am trying to create a static wrapper class to handle shared preferences of my app. I have created a helper class MyHelper
that contains another class Setting
which handles setting and getting values from app's shared preferences. I want to use the helper methods in this way-
MyHelper.Setting.saveBoolean("auto_start", true);
MyHelper.Setting.grabBoolean("auto_start");
Here goes my helper class.
public class MyHelper{
public static class Setting extends android.app.Application {
private static Context currentContext;
private static SharedPreferences preferences;
private static Editor updater;
@Override
public void onCreate(){
super.onCreate();
currentContext = this;
preferences = PreferenceManager
.getDefaultSharedPreferences(currentContext);
updater = preferences.edit();
}
public static boolean grabBoolean(String key) {
return preferences.getBoolean(key, false);
}
public static void saveBoolean(String key, boolean value) {
preferences.putBoolean(key, value).commit();
}
}
/*
* Some other nested classes here...
*/
}
I am getting NullPointerException
at preferences.getBoolean()
method. I presume preferences
is null at the time the method is called statically.
Is the approach of getting Application's context by extending android.app.Application
wrong? I would like to know what is the best approach to get the application's context from any service, activity, broadcast receiver or helper class used in an application.
I have gone through Android developers' reference and several other questions but none of those address this issue. Any guide, suggestion or exact solution to this problem would be highly appreciated.
Update: LOGCAT ERROR
12-04 12:14:47.467: E/AndroidRuntime(514): FATAL EXCEPTION: main
12-04 12:14:47.467: E/AndroidRuntime(514): java.lang.RuntimeException: Unable to start activity ComponentInfo{np.com.njs.statusinformer/np.com.njs.statusinformer.Setup}: java.lang.NullPointerException
12-04 12:14:47.467: E/AndroidRuntime(514): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-04 12:14:47.467: E/AndroidRuntime(514): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-04 12:14:47.467: E/AndroidRuntime(514): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-04 12:14:47.467: E/AndroidRuntime(514): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-04 12:14:47.467: E/AndroidRuntime(514): at android.os.Handler.dispatchMessage(Handler.java:99)
12-04 12:14:47.467: E/AndroidRuntime(514): at android.os.Looper.loop(Looper.java:123)
12-04 12:14:47.467: E/AndroidRuntime(514): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-04 12:14:47.467: E/AndroidRuntime(514): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 12:14:47.467: E/AndroidRuntime(514): at java.lang.reflect.Method.invoke(Method.java:507)
12-04 12:14:47.467: E/AndroidRuntime(514): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-04 12:14:47.467: E/AndroidRuntime(514): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-04 12:14:47.467: E/AndroidRuntime(514): at dalvik.system.NativeStart.main(Native Method)
12-04 12:14:47.467: E/AndroidRuntime(514): Caused by: java.lang.NullPointerException
12-04 12:14:47.467: E/AndroidRuntime(514): at np.com.njs.statusinformer.InformerHelper$Setting.grabBoolean(InformerHelper.java:115)
12-04 12:14:47.467: E/AndroidRuntime(514): at np.com.njs.statusinformer.Setup.loadPreferences(Setup.java:102)
12-04 12:14:47.467: E/AndroidRuntime(514): at np.com.njs.statusinformer.Setup.onCreate(Setup.java:31)
12-04 12:14:47.467: E/AndroidRuntime(514): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-04 12:14:47.467: E/AndroidRuntime(514): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-04 12:14:47.467: E/AndroidRuntime(514): ... 11 more
Here is my Manifest's
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="np.com.njs.statusinformer.BootCompleteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT" />
</intent-filter>
</receiver>
<service android:name=".InformerService" >
<intent-filter>
<action android:name="np.com.njs.statusinformer.InformerService" />
</intent-filter>
</service>
<activity
android:name="np.com.njs.statusinformer.Setup"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>