public static final String PREFS_NAME = "MyPrefsFile";
private SharedPreferences settings;
settings = getSharedPreferences(PREFS_NAME, 0);


当我在扩展应用程序的类中使用此代码时,在第3行中会得到一个nullpointer,而在活动中执行相同操作时它将起作用。为什么是这样?

我正在尝试创建用于容纳一些变量的单例类。仅10个变量,因此不足以使用sqllite左右。

编辑:
在我的第一个电话中,我得到一个DataClass单例实例:

   boolean active = DataClass.getInstance().getBoolean("active");


完整的DataClass:

import android.app.Application;
import android.content.SharedPreferences;
import android.util.Log;


public class DataClass extends Application  {
    public static final String PREFS_NAME = "MyPrefsFile";

    private static DataClass instance = null;
    public static DataClass getInstance() {
          if(instance == null) {
             instance = new DataClass();
          }
          return instance;
       }


    private SharedPreferences settings;

    public DataClass()  {
        settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);  //nullpointer here
        Log.e("temp", "DataClass constructor executed");
    }


    public void setBoolean(boolean trueorfalse, String id)      {
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(id, trueorfalse);
        editor.commit();
    }

    public boolean getBoolean(String id)        {
        return settings.getBoolean(id, false);
    }

}


编辑:添加了堆栈跟踪:

04-15 09:28:25.064: ERROR/AndroidRuntime(16943): FATAL EXCEPTION: main
04-15 09:28:25.064: ERROR/AndroidRuntime(16943): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.test.helloworld/be.test.helloworld.activities.MainActivity}: java.lang.NullPointerException
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at android.os.Looper.loop(Looper.java:143)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at android.app.ActivityThread.main(ActivityThread.java:4914)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at java.lang.reflect.Method.invoke(Method.java:521)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at dalvik.system.NativeStart.main(Native Method)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943): Caused by: java.lang.NullPointerException
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at be.test.helloworld.data.DataClass.<init>(DataClass.java:23)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at be.test.helloworld.data.DataClass.getInstance(DataClass.java:14)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at be.test.helloworld.activities.MainActivity.onCreate(MainActivity.java:29)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
04-15 09:28:25.064: ERROR/AndroidRuntime(16943):     ... 11 more

最佳答案

在您的onCreate()子类的Application中调用它

关于android - 在Application类中获取sharedPreferences时为NullPointer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5673526/

10-10 12:31