onSharedPreferenceChanged不更新首选项

时间:2014-01-24 18:12:56

标签: java android preferences

所以我在更改首选项时尝试更新timers.java类中的值。这是我的timers.java中的值

final boolean soundEnabledPref = sharedPref.getBoolean(spawnTimers.KEY_PREF_SOUND, false);

我在onCreate中调用了它,如果我更改了首选项,请返回2个屏幕到main.java,然后重新启动timers.java,更改将起作用,但是如果我只是进入首选项然后退回到计时器。 java更改未生效

这是我的settingsFragment.java

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceFragment;

public class settingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

    public static final String KEY_PREF_SOUND_SETTINGS = "pref_key_sounds_settings";

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.preferences);

        onSharedPreferenceChanged(null, "");
    }

    @Override
    public void onResume() {
        super.onResume();
        // Set up a listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {

        if (key.equals(KEY_PREF_SOUND_SETTINGS))
        {
            boolean soundEnabledPref = sharedPreferences.getBoolean(spawnTimers.KEY_PREF_SPAWNSOUND, false);
        }
    }



}
我猜猜我在onSharedPreferenceChanged中有什么不正确?不确定我需要将什么内容放入If语句中以将boolean的值更改回timers.java

任何帮助将不胜感激!

谢谢!

编辑:有人发布了一个解决问题的答案

我已将onSharedPreferencesChanged更改为

@Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {

        if (key.equals(KEY_PREF_SPAWN_SOUNDS))
        {
            CheckBoxPreference soundEnabledPref = (CheckBoxPreference)findPreference(KEY_PREF_SOUND_SETTINGS);
            soundEnabledPref.setChecked(sharedPreferences.getBoolean(spawnTimers.KEY_PREF_SPAWNSOUND, false));
        }
    }

现在更新该值并且它正在工作,但是,每当我选中或取消选中该框时,都会抛出错误

01-24 13:35:36.558: E/AndroidRuntime(3681): FATAL EXCEPTION: main
01-24 13:35:36.558: E/AndroidRuntime(3681): Process: com.rcd.helper, PID: 3681
01-24 13:35:36.558: E/AndroidRuntime(3681): java.lang.ClassCastException: android.preference.PreferenceCategory cannot be cast to android.preference.CheckBoxPreference
01-24 13:35:36.558: E/AndroidRuntime(3681):     at com.rcd.helper.settingsFragment.onSharedPreferenceChanged(settingsFragment.java:43)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:475)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.app.SharedPreferencesImpl$EditorImpl.apply(SharedPreferencesImpl.java:385)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.preference.Preference.tryCommit(Preference.java:1372)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.preference.Preference.persistBoolean(Preference.java:1638)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.preference.TwoStatePreference.setChecked(TwoStatePreference.java:84)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:70)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.preference.Preference.performClick(Preference.java:952)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.os.Handler.handleCallback(Handler.java:733)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.os.Looper.loop(Looper.java:137)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at android.app.ActivityThread.main(ActivityThread.java:5083)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at java.lang.reflect.Method.invokeNative(Native Method)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at java.lang.reflect.Method.invoke(Method.java:515)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-24 13:35:36.558: E/AndroidRuntime(3681):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

为什么要手动调用onSharedPreferenceChanged并将其传递为null并将String作为参数传递?

从代码中删除onSharedPreferenceChanged(null, "");