gpt4 book ai didi

android - 非字符串类型的默认 bindPreferenceSummaryToValue 崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:35:42 24 4
gpt4 key购买 nike

我正在按照示例方法添加找到的兼容首选项/fragment 对话框 here .这样做时,我发现如果我的首选项是整数、 bool 值等,它就会崩溃。

private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference
.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getString(preference.getKey(),""));
}

我发现我可以通过将 getString() 更改为 getInteger() 并根据类型使用不同的 bindPreferenceSummaryToValue 函数来为 Integer 工作是。当然,这似乎是一个非常不优雅的解决方案,但我正在努力弄清楚我还能做些什么。这是堆栈跟踪,顺便说一句。

11-22 19:52:10.068: E/AndroidRuntime(17564): FATAL EXCEPTION: main
11-22 19:52:10.068: E/AndroidRuntime(17564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kd7uiy.hamfinder/com.kd7uiy.hamfinder.MainSettingsActivity}: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.os.Looper.loop(Looper.java:137)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-22 19:52:10.068: E/AndroidRuntime(17564): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 19:52:10.068: E/AndroidRuntime(17564): at java.lang.reflect.Method.invoke(Method.java:525)
11-22 19:52:10.068: E/AndroidRuntime(17564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-22 19:52:10.068: E/AndroidRuntime(17564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-22 19:52:10.068: E/AndroidRuntime(17564): at dalvik.system.NativeStart.main(Native Method)
11-22 19:52:10.068: E/AndroidRuntime(17564): Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)
11-22 19:52:10.068: E/AndroidRuntime(17564): at com.kd7uiy.hamfinder.MainSettingsActivity.bindPreferenceSummaryToValue(MainSettingsActivity.java:194)

最佳答案

我找不到一个干净的解决方案,但最好的解决方法似乎是这样的,使用 instanceof 来确定您正在处理的数据类型。

private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference
.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);


if (preference instanceof SeekBarPreference)
{
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getInt(preference.getKey(),0));
}
else if (preference instanceof CheckBoxPreference)
{
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getBoolean(preference.getKey(),true));
}
}

关于android - 非字符串类型的默认 bindPreferenceSummaryToValue 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20157332/

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