android SharedPreferences 的问题,读不到数据。

尤德尼拉 尤德尼拉 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
		SharedPreferences share = getSharedPreferences("name", 0);
Editor e = share.edit();

//方法a,在第一句输出的时候读不到数据。
String a = "abc_cc";
System.err.println(share.getString(a, "默认结果"));//此句似乎没有执行,没有任何输出,错误...
//但是第二次执行的时候,会正常输出。
e.putString(a, "修改的结果");
e.commit();
System.err.println(share.getString(a, "默认结果"));//输出:修改的结果,正常

//方法b,一切正常
System.err.println(share.getString("abc_cc", "默认结果"));//输出:默认结果,正常
e.putString("abc_cc", "修改的结果");
e.commit();
System.err.println(share.getString("abc_cc", "默认结果"));//输出:修改的结果,正常

//求解,为什么方法a不行?



这是getString的源码
    String getString(String key, String defValue);

/**
* Retrieve a set of String values from the preferences.
*
* <p>Note that you <em>must not</em> modify the set instance returned
* by this call. The consistency of the stored data is not guaranteed
* if you do, nor is your ability to modify the instance at all.
*
* @param key The name of the preference to retrieve.
* @param defValues Values to return if this preference does not exist.
*
* @return Returns the preference values if they exist, or defValues.
* Throws ClassCastException if there is a preference with this name
* that is not a Set.
*
* @throws ClassCastException
*/
...全文

相关推荐

评论 10
收藏 已收藏
分享
返回顶部
尤德尼拉
Android 80,490 成员 91,375 内容