1

我正在使用共享首选项来存储有关是否使用布尔共享首选项为我的班级完成了某个目标的数据。当我尝试运行这部分代码时,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;
    }

}

感谢我能得到的任何帮助!

4

2 回答 2

0

既然你已经创建了一个 , 的子类Application来全局访问应用程序上下文,你还记得编辑你的 AndroidManifest.xml 吗?

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:name="MyApplication">   <!-- This line -->
于 2013-10-28T02:50:48.930 回答
0

该错误意味着 MyApplication.getAppContext() ==null,您可以检查您的代码 MyApplication getAppContext() 方法,

于 2013-10-28T01:54:35.143 回答