在Application类中获取sharedPreferences时的NullPointer

时间:2011-04-15 07:16:43

标签: android

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);
    }

}

编辑:添加了stacktrace:

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

2 个答案:

答案 0 :(得分:9)

onCreate()子类

Application中调用此方法

答案 1 :(得分:0)

此课程是否延长Activity课程!?

您可以尝试在第3行使用MODE_PRIVATE代替0吗?