gpt4 book ai didi

addPreferencesFromResource 上的 java.lang.Integer 无法转换为 java.lang.String

转载 作者:行者123 更新时间:2023-12-02 03:51:34 25 4
gpt4 key购买 nike

我有一个关于首选项的示例,如下所示:

public class UserPreferences extends PreferenceActivity {
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MynewFracment()).commit();
}
public static class MynewFracment extends PreferenceFragment{
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceStates);
addPreferencesFromResource(R.xml.userpreference);
}
}
}

但是返回的结果是 addPreferencesFromResource(R.xml.userpreferences); 行上的 java.lang.Integer Cannot be Cast to java.lang.String 。我真的不知道我在这里做错了什么?

<小时/>

更新 1

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="PREF_AUTO_UPDATE"
android:title="Auto refresh"
android:summary="Select to turn on automatic updateing"
android:defaultValue="true"
></CheckBoxPreference>
<ListPreference
android:key="PREF_UPDATE_FREQ"
android:title="Refresh frequency"
android:summary="Frequency at which to refresh earthquake list"
android:entries="@array/update_freq_option"
android:entryValues="@array/update_freq_values"
android:dialogTitle="Refresh frequency"
android:defaultValue="60"
></ListPreference>
<ListPreference
android:key="PREF_MIN_MAG"
android:title="Minimum magnitude"
android:summary="Select the minimum magnitude earthquake to report"
android:entries="@array/magnitude_options"
android:entryValues="@array/magnitude"
android:dialogTitle="Magnitude"
android:defaultValue="3"
></ListPreference>
</PreferenceScreen>
<小时/>

更新2

我的 R 值

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="update_freq_option">
<item>Every Minutes</item>
<item>5 mins</item>
<item>10 mins</item>
<item>15 mins</item>
<item>Every Hours</item>
</string-array>

<array name="magnitude">
<item>3</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
</array>

<string-array name="magnitude_options">
<item>3</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
</string-array>

<array name="update_freq_values">
<item>1</item>
<item>5</item>
<item>10</item>
<item>15</item>
<item>60</item>
</array>
</resources>

还有我的堆栈跟踪

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.begood.earthquake/com.begood.earthquake.UserPreferences}: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:205)
at android.preference.Preference.getPersistedString(Preference.java:1399)
at android.preference.ListPreference.onSetInitialValue(ListPreference.java:278)
at android.preference.Preference.dispatchSetInitialValue(Preference.java:1315)
at android.preference.Preference.onAttachedToHierarchy(Preference.java:1127)
at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156)
at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97)
at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38)
at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:269)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:278)
at com.begood.earthquake.UserPreferences$MynewFracment.onCreate(UserPreferences.java:20)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:796)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
at android.app.Activity.performStart(Activity.java:5017)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

最佳答案

正如您从堆栈跟踪中看到的,当您调用 addPreferencesFromResource 时,会加载首选项,并调用 onSetInitialValue 将首选项的值设置为保存的值之前,如果有的话。

因为它是一个 ListPreference,所以它使用 getPersistedString,它会尝试加载 String

这会引发 ClassCastException,因为 pref 曾经是 Integer 类型,并且是这样保存的。

您知道设备上现有的首选项版本与新版本的应用程序不兼容。

要解决此问题,请清除应用数据,或直接将其卸载。

如果您无法做到这一点(如果您的用户拥有该应用的先前版本),则您需要一个迁移策略,并且可能需要更改新首选项的名称以避免像这样的冲突。

关于addPreferencesFromResource 上的 java.lang.Integer 无法转换为 java.lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35833845/

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