开发者

NullPointer when getting sharedPreferences in Application class

开发者 https://www.devze.com 2023-02-24 19:10 出处:网络
public static final String PREFS_NAME = \"MyPrefsFile\"; private SharedPreferences settings; settings = getSharedPreferences(PREFS_NAME, 0);
public static final String PREFS_NAME = "MyPrefsFile";
private SharedPreferences settings;
settings = getSharedPreferences(PREFS_NAME, 0);

When I use this code in a class that Extends Application I get a nullpointer on line 3 while it works when I do the same in an activity. Why is this?

I'm trying to create a singleton class for holding a few variables. Only 10 variables so not enough to use sqllite or so.

edit: In my first call I get a DataClass singleton instance:

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

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

}

edit: added 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.handleLaun开发者_运维问答chActivity(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


Call this in onCreate() of your Application subclass


does this class extend Activity class!?

And could you try using MODE_PRIVATE instead of 0 on line 3!?

0

精彩评论

暂无评论...
验证码 换一张
取 消