【问题标题】:Creating SharedPreferences object inside Fragment在 Fragment 中创建 SharedPreferences 对象
【发布时间】:2013-07-04 12:06:27
【问题描述】:

我正在为我的同学开发一个可调整时间表的小型应用程序。它使用 Fragments 将一周中的每一天显示为一个漂亮的、可手指滑动的 UI:

在 SettingsActivity.class 中有首选项(在 xml 中定义),它们会自动将设置存储在 SharedPreferences 中。问题是定义 Fragment 的类是静态的。我不能在那里使用非静态方法引用,比如那个:

SharedPreferences settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);

通过阅读 developer.android.com 上的文档并搜索帮助,我发现要使用 SettingsActivity.class 创建的 SharedPreferences,我必须使用 PreferenceManager,如下所示:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

现在它没有显示关于非静态引用的错误,但我不知道如何在 getDefaultSharedPreferences 参数中引用 SettingsActivity.class,因为我在 Fragment 静态类中,所以我不能使用“this” .

我还尝试在静态类之外创建 SharedPreference 对象。但是,该对象的所有使用都抱怨“不能从静态上下文引用非静态字段”。

在那里使用 SharedPreferences 对我来说很重要,因为稍后我将根据一个小时来实现课程(TextViews)颜色变化,这也可以在“设置”中切换。

这是那个片段类的代码:

public static class Dzien extends Fragment {

    public static final String ARG_SECTION_NUMBER = "section_number";

    public Dzien() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView lekcja1 = (TextView) rootView.findViewById(R.id.lekcja1);
        TextView lekcja2 = (TextView) rootView.findViewById(R.id.lekcja2);
        TextView lekcja3 = (TextView) rootView.findViewById(R.id.lekcja3);
        TextView lekcja4 = (TextView) rootView.findViewById(R.id.lekcja4);
        TextView lekcja5 = (TextView) rootView.findViewById(R.id.lekcja5);
        TextView lekcja6 = (TextView) rootView.findViewById(R.id.lekcja6);
        TextView lekcja7 = (TextView) rootView.findViewById(R.id.lekcja7);
        TextView lekcja8 = (TextView) rootView.findViewById(R.id.lekcja8);
        TextView lekcja9 = (TextView) rootView.findViewById(R.id.lekcja9);


        //Here is where I try to create SharedPreference object
        SharedPreferences settings =  PreferenceManager.getDefaultSharedPreferences(this);


        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 1: { // Poniedziałek
                lekcja1.setText(getString(R.string.wos));
                lekcja2.setText(getString(R.string.mat));
                lekcja3.setText(getString(R.string.ang));
                lekcja4.setText(getString(R.string.gw));
                lekcja5.setText(getString(R.string.his));
                lekcja6.setText(getString(R.string.wf));
                lekcja7.setText(getString(R.string.pp));
                if (settings.getInt(GRUPA_INFORMATYKA, 1) == 1) {
                    lekcja8.setText(getString(R.string.inf));
                } else {
                    lekcja8.setText(getString(R.string.brak));
                }
                lekcja9.setText(getString(R.string.brak));
                break;
            }
            case 2: { // Wtorek
                lekcja1.setText(getString(R.string.mat));
                lekcja2.setText(getString(R.string.pp));
                lekcja3.setText(getString(R.string.rel));
                lekcja4.setText(getString(R.string.wf));
                lekcja5.setText(getString(R.string.pol));
                lekcja6.setText(getString(R.string.pol));
                if (settings.getInt(GRUPA_JEZYKOWA, 1) == 2) {
                    lekcja7.setText(getString(R.string.ros));
                    lekcja8.setText(getString(R.string.ros));
                } else {
                    lekcja7.setText(getString(R.string.brak));
                    lekcja8.setText(getString(R.string.brak));
                }
                lekcja9.setText(getString(R.string.brak));
                break;
            }
            case 3: { // Sroda
                lekcja1.setText(getString(R.string.his));
                lekcja2.setText(getString(R.string.wf));
                lekcja3.setText(getString(R.string.mat));
                lekcja4.setText(getString(R.string.rel));
                lekcja5.setText(getString(R.string.ang));
                if (settings.getInt(GRUPA_JEZYKOWA, 1) == 1) {
                    lekcja6.setText(getString(R.string.niem));
                    lekcja7.setText(getString(R.string.niem));
                } else {
                    lekcja6.setText(getString(R.string.brak));
                    lekcja7.setText(getString(R.string.brak));
                }
                lekcja8.setText(getString(R.string.brak));
                lekcja9.setText(getString(R.string.brak));
                break;
            }
            case 4: { // Czwartek
                lekcja1.setText(getString(R.string.mat));
                lekcja2.setText(getString(R.string.fiz));
                lekcja3.setText(getString(R.string.wok));
                lekcja4.setText(getString(R.string.geo));
                lekcja5.setText(getString(R.string.ang));
                lekcja6.setText(getString(R.string.chem));
                if (settings.getInt(GRUPA_INFORMATYKA, 1) == 2) {
                    lekcja7.setText(getString(R.string.inf));
                } else if (((settings.getInt(GRUPA_INFORMATYKA, 1) == 2)) && ((settings.getInt(GRUPA_JEZYKOWA, 1) != 3))) {
                    lekcja7.setText(getString(R.string.brakpre));
                } else {
                    lekcja7.setText(getString(R.string.brak));
                }
                if (settings.getInt(GRUPA_JEZYKOWA, 1) == 3) {
                    lekcja8.setText(getString(R.string.por));
                    lekcja9.setText(getString(R.string.por));
                }
                break;
            }
            case 5: { // Piątek
                lekcja1.setText(getString(R.string.mat));
                lekcja2.setText(getString(R.string.mat));
                lekcja3.setText(getString(R.string.bio));
                lekcja4.setText(getString(R.string.pol));
                lekcja5.setText(getString(R.string.pol));
                lekcja6.setText(getString(R.string.edb));
                if (settings.getBoolean(WDZ, false)) {
                    lekcja7.setText(getString(R.string.wdz));
                } else {
                    lekcja7.setText(getString(R.string.brak));
                }
                lekcja8.setText(getString(R.string.brak));
                lekcja9.setText(getString(R.string.brak));
                break;
            }


        }

        return rootView;
    }
}

附:我正在使用 Android Studio IDE。

【问题讨论】:

  • SharedPreferences 首选项 = this.getActivity().getSharedPreferences("pref",0);

标签: java android static android-fragments sharedpreferences


【解决方案1】:
PreferenceManager.getDefaultSharedPreferences(this);

要通过此方法获取SharedPreferences,您必须传递Context 对象。所以你可以在这种情况下传递你的Activity,因为Fragment 具有获取父Activity 的方法,而ActivityContext 的扩展类。代码如下所示:

PreferenceManager.getDefaultSharedPreferences(getActivity());

祝你好运!

【讨论】:

    【解决方案2】:

    改成这样:

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
    

    【讨论】:

      【解决方案3】:

      我被挂断了好几天,试图获取我的应用程序存储的 sharepreferences 密钥。我发现这篇关于查看存储在模拟器上的 xml sharepref 的技术说明。

      http://mrbool.com/how-to-implement-shared-preferences-in-android/28370

      这给了我关于我做错了什么的答案。 SharePref 的默认文件名似乎是 MainActivity.xml。所以下面的代码有效。参考

      私有静态字符串文件名 = "MainActivity"; pref = getSharedPreferences(文件名, 0); // 新的 8/27/14 测试

      【讨论】:

        猜你喜欢
        • 2016-03-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
        相关资源
        最近更新 更多