您的位置:首页 > Web前端

使用 Context.getSharedPreferences 读不到 xml 里面的内容,但在应用的 Settings 页面是正常可以更改 xml 内容

2014-10-17 10:26 411 查看
============问题描述============

Hi,

在 android 项目里面,在其中一个类(或多个类)里面使用以下方法取不到 Settings 页面里面的参数值,请帮忙看下是什么原因,谢谢。

下面的 result 返回值是 false, 但是我直接在手机上去到这个应用 data/data/com.example.app/shared_prefs/test_perference_file.xml 下打开这个 xml 文件,在 xml 里面对应的是 <boolean name="test_key" value="true"/> 这个值是 true .

请帮忙解释一下为什么会这样??

另外,如果我把下面代码的 defaultValue 改为 true ,则运行后 result 的值就会变成 true, 这个好像应该是只有在 xml 里面没有 "test_key" 这个值的时候才会赋默认值的吧??从这一点是否说明代码里面取得的 SharedPreferences 并没有加载到 data/data/com.example.app/shared_prefs/test_perference_file.xml 吗???

请教下是什么原因导致,谢谢。

public void test_function(Context context) {

// parse Preference file

SharedPreferences preferences = context.getSharedPreferences("test_perference_file", Context.MODE_PRIVATE);

// get values from Map

boolean defaultValue = false;

boolean result = preferences.getBoolean("test_key", defaultValue);

}


============解决方案1============

....getApplictionContext()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐