【问题标题】:SharedPreferences returns a null even though it haves a default valueSharedPreferences 返回 null,即使它具有默认值
【发布时间】:2013-01-29 14:35:14
【问题描述】:

我的主要活动是从 SharedPreferences 中保存和加载一些数据...直到现在我都能够做到这一点。

这些是我的 onCreate、onPause 和 OnResume 方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle(R.string.title_activity_main);

    this.spinner = (Spinner) findViewById(R.id.geomObject);
    this.sbWidth = (SeekBar) findViewById(R.id.widthSB);
    this.sbDepth = (SeekBar) findViewById(R.id.depthSB);
    this.sbHeight = (SeekBar) findViewById(R.id.heightSB);
    this.widthProgress = (TextView) findViewById(R.id.widthDisplay);
    this.depthProgress = (TextView) findViewById(R.id.depthDisplay);
    this.heightProgress = (TextView) findViewById(R.id.heightDisplay);

    ...more code...
}

protected void onResume() {
    super.onResume();
    loadPrefs();
}

protected void onPause() {
    super.onPause();
    savePrefs();
}

这些是我做大部分工作的方法,所以我可以保持 onX 方法干净:

private void savePrefs() {
    this.width = this.sbWidth.getProgress();
    this.depth = this.sbDepth.getProgress();
    this.height = this.sbHeight.getProgress();
    this.forma = this.spinner.getSelectedItemPosition();

    getSharedPreferences("SEEKBARS", MODE_PRIVATE).edit().putInt("width", width).commit();
    getSharedPreferences("SEEKBARS", MODE_PRIVATE).edit().putInt("depth", depth).commit();
    getSharedPreferences("SEEKBARS", MODE_PRIVATE).edit().putInt("height", height).commit();
    getSharedPreferences("SEEKBARS", MODE_PRIVATE).edit().putInt("forma", forma).commit();
    getSharedPreferences("LOCKBARS", MODE_PRIVATE).edit().putBoolean("lockbars", menuLock.isChecked()).commit();
}

private void loadPrefs() {
    int savedSpinner = getSharedPreferences("SEEKBARS", MODE_PRIVATE).getInt("forma", SPINNER_DEFAULT);
    int savedWidth = getSharedPreferences("SEEKBARS", MODE_PRIVATE).getInt("width", SEEKBAR_DEFAULT);
    int savedDepth = getSharedPreferences("SEEKBARS", MODE_PRIVATE).getInt("depth", SEEKBAR_DEFAULT);
    int savedHeight = getSharedPreferences("SEEKBARS", MODE_PRIVATE).getInt("height", SEEKBAR_DEFAULT);

    boolean savedLockFlag = getSharedPreferences("LOCKBARS", MODE_PRIVATE).getBoolean("lockbars", false);

    this.spinner.setSelection(savedSpinner);
    this.sbWidth.setProgress(savedWidth);
    this.sbDepth.setProgress(savedDepth);
    this.sbHeight.setProgress(savedHeight);
//This is the line raising the exception Display:
        this.menuLock.setChecked(savedLockFlag); 


        this.widthProgress.setText(String.valueOf(savedWidth) + " " + getResources().getString(R.string.blocks));
        this.depthProgress.setText(String.valueOf(savedDepth) + " " + getResources().getString(R.string.blocks));
        this.heightProgress.setText(String.valueOf(savedHeight) + " " + getResources().getString(R.string.blocks));
    }

我看不出错误在哪里。我一直在寻找,我知道我不能在 onCreate 上调用 getSharedPreferences,这就是我在 onResume 上这样做的原因。怎么了?

我还附上了logcat,以防万一:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {es.nirvash.android.msg/es.nirvash.android.msg.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2567)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2595)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2109)
    at android.app.ActivityThread.access$600(ActivityThread.java:132)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4575)
    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:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at es.nirvash.android.msg.MainActivity.loadPrefs(MainActivity.java:231)
    at es.nirvash.android.msg.MainActivity.onResume(MainActivity.java:147)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
    at android.app.Activity.performResume(Activity.java:4539)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2557)
    ... 12 more

【问题讨论】:

  • "我没有看到错误在哪里。我一直在搜索,我知道我不能在 onCreate 上调用 getSharedPreferences,这就是我在 onResume 上这样做的原因"你从哪里听到这个?错了。
  • 这里:stackoverflow.com/a/5991458/1569084 "getSharedPreferences() 只能在 Activity 调用 onCreate() 之后调用。"
  • 使用调试器。看看到底什么是空的。
  • 您对这个答案的解释是错误的。 getSharedPreferences() 必须在系统调用 onCreate 之后调用,也就是说你可以在 onCreate 期间调用它。
  • @Deses - 我认为他们的意思是说你只能在调用 super.onCreate(bundle) 之后调用它。你当然可以在 onCreate 方法中调用它。

标签: android null nullpointerexception sharedpreferences


【解决方案1】:

问题不是你的布尔值,而是变量menuLockboolean(原始类型)不能是null

找到menuLock 的声明位置,并确保它有一些东西可以保留。这样,当你使用它时它就不会为空。当然,如果它是 View,请在 onCreate() 中,在 setContentView() 之后为其分配一些要保留的内容,因为它具有 Activity 布局和创建保证。

【讨论】:

  • 天哪,就是这样。该死的!我忘记了 menuLock 在其他地方被删除了。
  • 不客气 :-) 正如我在回答中所说,原始类型(char、int、boolean、double、float)不能是null。这是为参考变量保留的,因此请务必检查。
  • 是的,我知道。有时我忘了启动变量。
猜你喜欢
  • 1970-01-01
  • 2018-10-22
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多