Creating a Preference Activity In Android
<p>I'm trying to create a simple preferences activity screen, following the <a href="http://developer.android.com/reference/android/preference/PreferenceScreen.html" rel="nofollow">docs</a>. I've set it up to launch when an options menu button is pressed, but when I trigger it, my app crashes and I get this useless runtime exception message:</p>

<pre><code>07-29 21:42:28.879: E/AndroidRuntime(13441): FATAL EXCEPTION: main
07-29 21:42:28.879: E/AndroidRuntime(13441): java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp/myapp.ApplicationPreferenceActivity}: java.lang.ClassCastException: java.lang.Integer
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread.access$2500(ActivityThread.java:129)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.os.Looper.loop(Looper.java:143)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread.main(ActivityThread.java:4701)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at java.lang.reflect.Method.invokeNative(Native Method)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at java.lang.reflect.Method.invoke(Method.java:521)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at dalvik.system.NativeStart.main(Native Method)
07-29 21:42:28.879: E/AndroidRuntime(13441): Caused by: java.lang.ClassCastException: java.lang.Integer
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ContextImpl$SharedPreferencesImpl.getString(ContextImpl.java:2797)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.Preference.getPersistedString(Preference.java:1249)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.ListPreference.onSetInitialValue(ListPreference.java:232)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.Preference.dispatchSetInitialValue(Preference.java:1172)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at andr
oid.preference.Preference.onAttachedToHierarchy(Preference.java:984)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.GenericInflater.inflate(GenericInflater.java:326)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.GenericInflater.inflate(GenericInflater.java:263)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at myapp.ApplicationPreferenceActivity.onCreate(ApplicationPreferenceActivity.java:71)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-29 21:42:28.879: E/AndroidRuntime(13441):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
07-29 21:42:28.879: E/AndroidRuntime(13441):    ... 11 more
</code></pre>

<p>My preferences configuration file is simply:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:title="@string/preferences_title" &gt;
    &lt;ListPreference
        android:defaultValue="1"
        android:entries="@array/units_choices"
        android:entryValues="@array/units_values"
        android:key="units"
        android:summary="@string/units_summary"
        android:title="@string/units_title" /&gt;
&lt;/PreferenceScreen&gt;
</code></pre>

<p>I have the units_choices and units_values defined in res/values/array.xml and res/values/array_values.xml respectively, which are simply:</p>

<pre><code>&lt;resources
   xmlns:android="http://schemas.android.com/apk/res/android"&gt;
   &lt;string-array
      name="units_choices"&gt;
      &lt;item&gt;Imperial (miles)&lt;/item&gt;
      &lt;item&gt;Metric (meters)&lt;/item&gt;
   &lt;/string-array&gt;
&lt;/resources&gt;

&lt;resources
    xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    &lt;string-array name="units_values" translatable="false"&gt;
        &lt;item&gt;2&lt;/item&gt;
        &lt;item&gt;1&lt;/item&gt;
    &lt;/string-array&gt;
&lt;/resources&gt;
</code></pre>

<p>If I comment out the ListPreference tag, the preference screen shows and my app doesn't crash, so I'm assuming the ListPreference is configured incorrectly. What am I doing wrong? I'm developing using the Eclipse IDE, and it's showing that all the @ references are satisfied. Why would this single tag crash the entire app?</p>
