在共享首选项中存储首选项

时间:2018-10-05 13:19:49

标签: java android sharedpreferences settings

我正在将设置部分添加到我的Android应用中。除了一些静态设置之外,本节还提供了一个选项,用于动态添加更多首选项子节。

enter image description here

如图所示,通过单击“添加设备”,我可以添加其他首选项,这又使我可以在相应的首选项小节中进行进一步设置。但是,我无法使这些动态偏好永久化。我试图将Preference对象序列化为String以便将它们保存在SharedPreferences中,但是系统会抛出此异常:

java.lang.SecurityException: Can not make a java.lang.reflect.Method constructor accessible

这是我写的代码:

Preference addDeviceBtn = (Preference)findPreference("add_btn");
    if (addDeviceBtn != null) {
        addDeviceBtn.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference arg0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("Device Name");
                final EditText input = new EditText(getContext());
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
                input.setLayoutParams(lp);
                builder.setView(input);

                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String deviceName = input.getText().toString();

                        SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
                        if (!appSharedPrefs.contains(deviceName)) {

                            Preference preference = new Preference(getContext());
                            preference.setTitle(deviceName);
                            getPreferenceScreen().addPreference(preference);
                            preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                                @Override
                                public boolean onPreferenceClick(Preference preference) {

                                    mListener.onDeviceSelected(deviceName);
                                    return false;
                                }
                            });

                            SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
                            Gson gson = new Gson();
                            String json = gson.toJson(preference); // <--- this is the problem
                            prefsEditor.putString("device_"+deviceName, json);
                            prefsEditor.apply();
                        } else
                            Toast.makeText(getActivity(), "Device already added", Toast.LENGTH_SHORT).show();
                    }
                });

                builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

                builder.show();
                return true;
            }
        });
    }

可以帮我吗?预先感谢。

0 个答案:

没有答案