我正在使用共享首选项来存储数据是否为我的类使用布尔共享首选项完成某个目标。当我尝试运行我的代码这部分的logcat打印出这一点:获取运行时异常试图访问sharedprefs的布尔android
10-28 00:35:09.771: E/AndroidRuntime(429): FATAL EXCEPTION: main
10-28 00:35:09.771: E/AndroidRuntime(429): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.selectstartgo.physics281/com.selectstartgo.physics281.Grading}: java.lang.NullPointerException
10-28 00:35:09.771: E/AndroidRuntime(429): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-28 00:35:09.771: E/AndroidRuntime(429): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-28 00:35:09.771: E/AndroidRuntime(429): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-28 00:35:09.771: E/AndroidRuntime(429): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-28 00:35:09.771: E/AndroidRuntime(429): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 00:35:09.771: E/AndroidRuntime(429): at android.os.Looper.loop(Looper.java:123)
10-28 00:35:09.771: E/AndroidRuntime(429): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-28 00:35:09.771: E/AndroidRuntime(429): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 00:35:09.771: E/AndroidRuntime(429): at java.lang.reflect.Method.invoke(Method.java:507)
10-28 00:35:09.771: E/AndroidRuntime(429): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-28 00:35:09.771: E/AndroidRuntime(429): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-28 00:35:09.771: E/AndroidRuntime(429): at dalvik.system.NativeStart.main(Native Method)
10-28 00:35:09.771: E/AndroidRuntime(429): Caused by: java.lang.NullPointerException
10-28 00:35:09.771: E/AndroidRuntime(429): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:353)
10-28 00:35:09.771: E/AndroidRuntime(429): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:348)
10-28 00:35:09.771: E/AndroidRuntime(429): at com.selectstartgo.physics281.PhysSharedPrefs.getSharPrefBoolean(PhysSharedPrefs.java:36)
10-28 00:35:09.771: E/AndroidRuntime(429): at com.selectstartgo.physics281.Grading.findCLevel(Grading.java:270)
10-28 00:35:09.771: E/AndroidRuntime(429): at com.selectstartgo.physics281.Grading.initialize(Grading.java:24)
10-28 00:35:09.771: E/AndroidRuntime(429): at com.selectstartgo.physics281.Grading.onCreate(Grading.java:16)
10-28 00:35:09.771: E/AndroidRuntime(429): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-28 00:35:09.771: E/AndroidRuntime(429): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
这里是我的共享偏好管理器
package com.selectstartgo.physics281;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
public class PhysSharedPrefs {
public static void putSharPrefInt(Context context, String key, int value) {
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(context);
Editor edit = pref.edit();
edit.putInt(key, value);
edit.commit();
}
public static void putSharPrefBoolean(Context context, String key,
boolean value) {
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(context);
Editor edit = pref.edit();
edit.putBoolean(key, value);
edit.commit();
}
public static int getSharPrefInt(Context context, String key, int _default) {
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(context);
return pref.getInt(key, _default);
}
public static boolean getSharPrefBoolean(Context context, String key,
boolean _default) {
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(context);
return pref.getBoolean(key, _default);
}
}
和这里的不工作
if (PhysSharedPrefs.getSharPrefBoolean(MyApplication.getAppContext(),
"bKey101", false))
i++;
if (PhysSharedPrefs.getSharPrefBoolean(MyApplication.getAppContext(),
"bKey102", false))
i++;
if (PhysSharedPrefs.getSharPrefBoolean(MyApplication.getAppContext(),
"bKey103", false))
i++;
if (PhysSharedPrefs.getSharPrefBoolean(MyApplication.getAppContext(),
"bKey104", false))
的代码块
而我的MyApplication代码如下所示:
package com.selectstartgo.physics281;
import android.app.Application;
import android.content.Context;
public class MyApplication extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
MyApplication.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApplication.context;
}
}
感谢您的帮助!
package com.selectstartgo.physics281; import android.app.Application; import android.content.Context; 公共类MyApplication扩展应用程序{0}私有静态上下文上下文; \t public void onCreate(){ \t \t super.onCreate(); \t \t MyApplication.context = getApplicationContext(); \t} \t公共静态上下文getAppContext(){ \t \t返回MyApplication.context; \t} } 这是我的MyApplication代码...我不知道它是如何等于null仍然。 –