gpt4 book ai didi

java - onSharedPreferenceChanged 不更新首选项

转载 作者:行者123 更新时间:2023-12-01 13:35:09 28 4
gpt4 key购买 nike

因此,当首选项更改时,我尝试更新timers.java 类中的值。这是我的timers.java中的值

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

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

这是我的settingFragment.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)

最佳答案

为什么要手动调用 onSharedPreferenceChanged 并将 null 和空字符串作为参数传递给它?

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

关于java - onSharedPreferenceChanged 不更新首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21339543/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com