我使用共享首选项来存储数据,以确定是否使用布尔共享首选项为我的类完成了某个目标。当我尝试运行我的代码的这部分时,logcat打印出来:
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;
}
}
感谢您的帮助!
答案 0 :(得分:0)
错误意味着MyApplication.getAppContext()== null,您可以检查您的代码MyApplication getAppContext()方法,
答案 1 :(得分:0)
由于您创建了Application
的子类,为了全局访问应用程序上下文,您是否记得编辑AndroidManifest.xml?
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name="MyApplication"> <!-- This line -->