简体   繁体   中英

Creating a Preference Activity In Android

I'm trying to create a simple preferences activity screen, following the docs . 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:

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

My preferences configuration file is simply:

<?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>

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

<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>

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?

This isn't an answer, but was a bit big for a comment.

I assume myapp.ApplicationPreferenceActivity.onCreate(ApplicationPreferenceActivity.java:71) is the line of your ApplicationPreferenceActivity where you are calling setContentView()? Thus it is an xml inflate error.

A couple of things about your xml file. You don't need to call:

android:layout_width="match_parent"
android:layout_height="match_parent"

Like in the example from the docs .

Also, what API level are you using? Are you aware that from API level 11 much of PreferenceActivity class has been deprecated, and now it uses PreferenceFragments instead? So if you want to support Pre Android 3.0 you can use PreferenceActivity, however, if you want to support 3.0 and above you'll have to use PreferenceFragments .

1. First of all i see a ClassCastException , that will happen when you try to push in an object of wrong type into the Object Reference Variable of UN-Compatible type. As you have not put up the code over here, i am unable to detect it.

2. As you are now learning about Preference Activity, i will suggest you to see this link

http://androidresearch.wordpress.com/2012/03/09/creating-a-preference-activity-in-android/

The problem was caused by my setting a default unit preference using putInt in my activity's onCreate, whereas the units_values are apparently interpreted as strings even though they're also integers. When the ListPreference tried to load my default int as a string, that threw the ClassCastException.

I modified my code in onCreate to use putString, cleared my installed app's data, and that fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM