【问题标题】:Multiple shared preferences causing default values导致默认值的多个共享首选项
【发布时间】:2013-11-23 21:22:48
【问题描述】:

基本上,我正在制作一个计算不同金额的复利的应用程序。如果您愿意,有 2 个“共享偏好”。

设置 - 供用户输入利率、复利频率、期限

首选项 - 用于应用设置(例如,是否显示美分,设置背景颜色)

我拥有的程序是当我有一组偏好时它运行良好。然而,当我有两个时(当我创建“首选项”时),它搞砸了第一个设置首选项,称为“设置”。

此外,每当它检索到首选项时,它都在一个片段中。

这是我获得首选项的方式(当我没有 2 个首选项 xml 时):

SharedPreferences getSetup = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
String t2 = getSetup.getString("rate", null);

这是我尝试过的解决方案(打开特定文件,然后当然使用 getSetup 或 getPrefs.getString("key, null") - 它不起作用,总是返回默认值)

SharedPreferences getPrefs = getActivity().getSharedPreferences("preferences",0); 和设置

SharedPreferences getSetup = getActivity().getSharedPreferences("setup",0);

每当我运行应用程序时,我都会收到错误消息(“请正确设置应用程序并输入所有值”)此错误消息设置为使用以下代码运行:

SharedPreferences getSetup = getActivity().getSharedPreferences("setup",0);    
int t1 = value1.getText().length();
int t1_2 = value2.getText().length();

String t2 = getSetup.getString("rate", null);
String t3 = getSetup.getString("retirement", null);

String t4 = getSetup.getString("compounds", null);
String t5 = result.getText().toString();

if (t1 == 0 ||t1_2 ==0 || t2 == null || t3 == null || t4 == null) {
 //show the error message

谁能告诉我我做错了什么以及如何获得特定的偏好设置。

干杯

【问题讨论】:

    标签: android android-fragments sharedpreferences android-xml android-preferences


    【解决方案1】:

    我想你想要的是

    SharedPreferences getSetup = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    

    您可以在以下位置查看有关上下文的更多信息 What's the difference between the various methods to get a Context?

    【讨论】:

    • 我收到错误“方法 getApplicationContext() 未定义新 View.OnClickListener(){} 类型”,或者如果我把它放在开头,方法 getApplicationContext() 未定义输入 FragmentNormal
    • 您能否在获取上下文的位置显示更多代码?并显示类声明。
    • public class FragmentNormal 扩展了 Fragment,我尝试了您的解决方案,但使用 getActivity().getApplicationContext(),应用程序在尝试返回值时退出。围绕上下文,在我有两个单独的首选项文件之前,我总是使用 (getActivity().getBaseContext())
    • 嗯,你从系统得到什么错误(不是你显示的错误)。堆栈跟踪会有所帮助,包括代码示例中发生故障的指示符。
    • 我没有从系统中得到任何错误 - 在我的代码中,它只有在所有值都存在时才会移动。我现在遇到的问题是,即使我指定了默认值(例如 null - String t2 = getSetup.getString("rate", null); 如果它为空,它仍然返回“”,而不是 null
    【解决方案2】:

    getDefaultSharedPreferences 和 getSharedPreferences 有区别 请按照以下方式尝试。

    此 defaultsharedpref 用于应用特定的共享首选项。

    SharedPreferences getSetup = PreferenceManager.getDefaultSharedPreferences(getActivity());
            getSetup.edit().putString("rate", "200").commit();
            String rate= getSetup.getString("rate", null);
    

    SharedPreferences 存储在应用数据文件夹中的 xml 文件中,即

    /data/data/YOUR_PACKAGE_NAME/shared_prefs/reference.xml /data/data/YOUR_PACKAGE_NAME/shared_prefs/setup.xml

    或默认首选项:

    /data/data/YOUR_PACKAGE_NAME/shared_prefs/com.android.example_preferences.xml

    【讨论】:

    • 我得到“FragmentNormal 类型的方法 getSharedPreferences(String, int) 未定义”。 setup 也是 XML 文件的名称,而不是其中的特定首选项。
    • 当应用程序创建这将创建默认的首选项文件。您正在保存的首选项是另一个文件(不在默认的 o.e 首选项或设置中)并从默认的共享首选项文件中检索。这就是您获得默认设置值的原因,即 null ...如果它澄清了您的问题,请告诉我
    • 如果可能的话,请查看下面的链接stackoverflow.com/questions/6146106/…
    • 感谢您的帮助,我已经整理好了。我只是不太明白偏好是如何运作的。
    猜你喜欢
    • 2013-07-19
    • 2013-06-19
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多