【发布时间】:2013-12-20 13:40:54
【问题描述】:
我正在尝试创建一个静态包装类来处理我的应用程序的共享首选项。我创建了一个帮助类 MyHelper,其中包含另一个类 Setting,它处理从应用程序的共享首选项设置和获取值。我想以这种方式使用辅助方法-
MyHelper.Setting.saveBoolean("auto_start", true);
MyHelper.Setting.grabBoolean("auto_start");
这是我的助手类。
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...
*/
}
我在preferences.getBoolean() 方法中得到NullPointerException。我假设 preferences 在静态调用该方法时为空。
通过扩展android.app.Application 获取应用程序上下文的方法是否错误?我想知道从应用程序中使用的任何服务、活动、广播接收器或助手类获取应用程序上下文的最佳方法是什么。
我查看了 Android 开发人员的参考资料和其他几个问题,但没有一个能解决这个问题。对此问题的任何指导、建议或确切的解决方案将不胜感激。
更新:LOGCAT 错误
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
这是我的清单
<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>
【问题讨论】:
-
首选项是
null! -
我已经更新了问题(添加了 logcat 错误)。
-
首选项为空!!!您应该将 AndroidManifest.xml 中的 App 替换为您的设置。像这样:
-
我已经初始化了首选项字段:preferences = PreferenceManager .getDefaultSharedPreferences(currentContext);
-
@easton 我的应用程序中有几个类。这真的有用吗?
标签: android nullpointerexception sharedpreferences