我正在尝试按照文档创建一个简单的首选项活动屏幕。我已经将它设置为在按下选项菜单按钮时启动,但是当我触发它时,我的应用程序崩溃并且我收到了这个无用的运行时异常消息:
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
我的首选项配置文件很简单:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<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" />
</PreferenceScreen>
我分别在 res/values/array.xml 和 res/values/array_values.xml 中定义了 units_choices 和 units_values,它们很简单:
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<string-array
name="units_choices">
<item>Imperial (miles)</item>
<item>Metric (meters)</item>
</string-array>
</resources>
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<string-array name="units_values" translatable="false">
<item>2</item>
<item>1</item>
</string-array>
</resources>
如果我注释掉 ListPreference 标记,则会显示首选项屏幕并且我的应用程序不会崩溃,所以我假设 ListPreference 配置不正确。我究竟做错了什么?我正在使用 Eclipse IDE 进行开发,它表明所有 @ 引用都得到满足。为什么这个单一标签会使整个应用程序崩溃?