我正在尝试从Reto Meier的Android 2 Application Development书籍中获得一个示例来工作(第202页)。按照说明,我创建了一个userpreferences.xml,如下所示:

<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 updating"
    android:defaultValue="true"
   />
  <ListPreference
    android:key="PREF_UPDATE_FREQ"
    android:title="Refresh frequency"
    android:summary="Frequency at which to refresh earthquake list"
    android:entries="@array/update_freq_options"
    android:entryValues="@array/update_freq_values"
    android:dialogTitle="Refresh frequency"
    android:defaultValue="60"
  />
  <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"
  />
</PreferenceScreen>

我的偏好设置类如下所示...
public class Preferences extends PreferenceActivity {
  SharedPreferences prefs;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.userpreferences);
  }

我不断收到ClassCastException:java.lang.ClassCastException:java.lang.Integer
12-16 09:28:14.349: ERROR/AndroidRuntime(287): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dt.sample/com.dt.sample.Preferences}: java.lang.ClassCastException: java.lang.Integer
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.os.Looper.loop(Looper.java:123)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at java.lang.reflect.Method.invokeNative(Native Method)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at java.lang.reflect.Method.invoke(Method.java:521)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at dalvik.system.NativeStart.main(Native Method)
12-16 09:28:14.349: ERROR/AndroidRuntime(287): Caused by: java.lang.ClassCastException: java.lang.Integer
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.app.ContextImpl$SharedPreferencesImpl.getString(ContextImpl.java:2699)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.Preference.getPersistedString(Preference.java:1249)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.ListPreference.onSetInitialValue(ListPreference.java:232)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.Preference.dispatchSetInitialValue(Preference.java:1172)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.Preference.onAttachedToHierarchy(Preference.java:984)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.GenericInflater.inflate(GenericInflater.java:326)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.GenericInflater.inflate(GenericInflater.java:263)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at com.dt.sample.Preferences.onCreate(Preferences.java:24)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-16 09:28:14.349: ERROR/AndroidRuntime(287):     ... 11 more

arrays.xml看起来像这样...
<resources>
  <string-array name="update_freq_options">
    <item>Every Minute</item>
    <item>5 minutes</item>
    <item>10 minutes</item>
    <item>15 minutes</item>
    <item>Every Hour</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>

我尝试使用整数数组,但这没有帮助。我究竟做错了什么?请帮忙。谢谢。

最佳答案

我遇到了同样的问题-书中上一个示例中的用户首选项数据在保存首选项数据时使用了不同的数据类型。

解决方案是在启动AVD时简单地选中“清除用户数据”复选框以摆脱旧的用户首选项。

关于android - PreferenceActivity中的ClassCastException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4463886/

10-10 23:33