I am trying to get an example from the Android 2 Application Development book by Reto Meier to work (page 202). As per the instructions I have created a userpreferences.xml as follows:
<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>
My Preferences class looks like this...
public class Preferences extends PreferenceActivity {
SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.userpreferences);
}
I keep getting a 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
The arrays.xml looks like this...
<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>
I tried using integer-array but that didn't help. What am I doing wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…