android开发入门教程

android开发入门教程
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

============问题描述============


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()
我要啦免费统计