SharedPreferences null指针异常

时间:2013-10-10 21:56:13

标签: android android-sharedpreferences

我正在尝试通过getSharePreferences加载一些数据。我已将保存和还原放在可以从不同活动访问的单独类中。当我只用一个来测试。但每次我运行应用程序时都会崩溃。


致电代码:

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

            LoadValues();

    } 
    public void LoadValues(){
         cs_settings settings = new cs_settings();
         settings.setContext(this)
         TargetEnd.setText(String.valueOf(settings.getTargetEnd()));
          TargetBeg.setText(String.valueOf(settings.getTargetBegining()));

    }

cs_settings:

public class cs_settings {

    private float TargetBegining, TargetEnd;

    public static final String PREFS_NAME = "settingsFile";
    private Context context;

    public void setContext(Context context) {
        this.context = context;
    }

    SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();



    public float getTargetBegining() {
        TargetBegining = settings.getFloat("targetbeg", 0f);
        return TargetBegining;
    }

    public void setTargetBegining(float targetBegining) {
        TargetBegining = targetBegining;
        editor.putFloat("targetbeg", TargetBegining);
        editor.commit();
    }

    public float getTargetEnd() {
        TargetEnd = settings.getFloat("targetEnd", 0f);
        return TargetEnd;
    }

    public void setTargetEnd(float targetEnd) {
        TargetEnd = targetEnd;
        editor.putFloat("targetEnd", TargetEnd);
        editor.commit();
    }

}

错误:

10-10 22:53:54.806      223-223/? E/installd﹕ ECS_ECS: * do_linklib  pkg [net.malcomber.insulincorrect]
10-10 22:53:54.806      223-223/? E/installd﹕ ECS_ECS: * do_dexopt  pkg [/data/app/net.malcomber.insulincorrect-2.apk]
10-10 22:53:55.356      223-223/? E/installd﹕ ECS_ECS: * do_move_dex  pkg [/data/app/net.malcomber.insulincorrect-2.apk]
10-10 22:53:58.069    8212-8212/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{net.malcomber.insulincorrect/net.malcomber.insulincorrect.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
            at android.app.ActivityThread.access$700(ActivityThread.java:152)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5328)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at net.malcomber.insulincorrect.cs_settings.<init>(cs_settings.java:20)
            at net.malcomber.insulincorrect.MainActivity.LoadValues(MainActivity.java:110)
            at net.malcomber.insulincorrect.MainActivity.onCreate(MainActivity.java:76)
            at android.app.Activity.performCreate(Activity.java:5250)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
            at android.app.ActivityThread.access$700(ActivityThread.java:152)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5328)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

查看班级的属性:

private Context context;
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();

settings提供值时,contextnull。所以你得到一个NullPointerException。

您可能想要创建一个构造函数:

private Context context;
SharedPreferences settings;
SharedPreferences.Editor editor;

public cs_settings(Context context) {
    this.context = context;
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    editor = settings.edit();
}