【问题标题】:sharedPreferences return null valuesharedPreferences 返回空值
【发布时间】:2016-06-18 01:16:29
【问题描述】:

//在一个片段中

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());

SharedPreferences.Editor editor = preferences.edit();
editor.putString("aKeyString", "aValueString");
editor.apply()

//If I try to log the just saved value the log is empty. Though I thought the apply(); committed that value to memory instantly and later to disc. So should be readable ?

Log.d(TAG, preferences.getString("aKeyString",""));
//nothing is logged to logcat at all. Not even a blank line. 

但是,我需要在另一个 Fragment 中读取该值但返回 null。

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
Log.d(TAG, "value: " + preferences.getString("aKeyString",""));
//Logs out "value: null"

为什么它是空的?我正在使用getDefaultSharedPreferences,这将确保我访问正确的数据和getActivity().getApplicationContext(),原因相同。

关于 SO 有关返回 null 的首选项的类似问题的帖子建议使用:

'应用();'而不是“提交();”我已经是了。 要么 他们建议使用 'getDefaultSharedPreferences' 而不是 'getSharedPreferences' 我也是。

为什么它是空的?

【问题讨论】:

  • @Saveen apply()commit() 快。
  • getActivity() 可能为 null 有检查吗?
  • commit 会阻止 UI,同时它会立即保存并应用保存到内存,以便可以使用然后异步到磁盘。
  • 好的,谢谢@RyanTCB 和 Bob Malooga
  • @BobMalooga 是的,错字已更正,谢谢

标签: android android-fragments sharedpreferences


【解决方案1】:

你做对了,顺便说一句,奇怪的是你说它显示“null”,而你指定了一个默认的空字符串值,根据javadoc这应该是不可能的:

/**
     * Retrieve a String value from the preferences.
     * 
     * @param key The name of the preference to retrieve.
     * @param defValue Value to return if this preference does not exist.
     * 
     * @return Returns the preference value if it exists, or defValue.  Throws
     * ClassCastException if there is a preference with this name that is not
     * a String.
     * 
     * @throws ClassCastException
     */
    @Nullable
    String getString(String key, @Nullable String defValue);

你确定它不会在这里抛出异常并解释为什么你看不到日志行吗?

我建议你使用

布尔提交();

并检查返回值。不幸的是,apply 方法会将其更改提交到内存中,但会启动异步提交到磁盘,并且您不会收到任何失败的通知。

【讨论】:

  • 把代码是if语句检查editor.commit();布尔值,它可以工作。不知道为什么?不会是模拟器问题吧?
【解决方案2】:

我认为你应该使用活动的上下文,而不是getApplicationContext()

两者都是 Context 的实例,但应用实例与应用的生命周期绑定,而 Activity 实例与 Activity 的生命周期绑定。因此,他们可以访问有关应用程序环境的不同信息。

你可以在fragment里面获取activity相关的上下文,比如这样:

private Context ctx;

 @Override
 public void onAttach(Context context) {
 super.onAttach(context);
 ctx = context;
 }

然后在你需要的地方做:

preferences = PreferenceManager.getDefaultSharedPreferences(ctx);

【讨论】:

    【解决方案3】:

    使用此代码

     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
    

    SharedPreferences.Editor 编辑器=preferences.edit();

    editor.putString("aKeyString", "aValueString"); editor.commite()

    【讨论】:

    • 谢谢,添加 editor.commit();也有效。但是,从 2.3 开始,人们建议使用 apply()
    猜你喜欢
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多