PreferenceActivity的ClassCastException

时间:2012-11-19 00:17:11

标签: android android-preferences

每次尝试使用此首选项时,我都会遇到classCastException,而我无法弄清楚原因。任何帮助将非常感激。崩溃的行是addPreferencesFromResources命令。

import android.os.Bundle;
import android.preference.PreferenceActivity;


public class PreferenceManager extends PreferenceActivity {
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences); // This line crashes
    }
}

preference.xml

<?xml version="1.0" encoding="utf-8"?>
<Preference xmlns:android="http://schemas.android.com/apk/res/android" >
                <CheckBoxPreference
                        android:title="@string/startAnimation"
                        android:defaultValue="true"
                        android:summary="@string/startAnimationDescription"
                        android:key="startAnimation" />    

</Preference>

堆栈跟踪

11-18 19:04:19.165: E/AndroidRuntime(1362): Uncaught handler: thread main exiting due to uncaught exception
11-18 19:04:19.175: E/AndroidRuntime(1362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pearsonartphoto.psychictester/com.pearsonartphoto.psychictester.PreferenceManager}: java.lang.ClassCastException: android.preference.Preference
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.os.Looper.loop(Looper.java:123)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.app.ActivityThread.main(ActivityThread.java:4363)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at java.lang.reflect.Method.invoke(Method.java:521)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at dalvik.system.NativeStart.main(Native Method)
11-18 19:04:19.175: E/AndroidRuntime(1362): Caused by: java.lang.ClassCastException: android.preference.Preference
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.preference.GenericInflater.inflate(GenericInflater.java:320)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.preference.GenericInflater.inflate(GenericInflater.java:263)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:254)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:253)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at com.pearsonartphoto.psychictester.PreferenceManager.onCreate(PreferenceManager.java:12)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-18 19:04:19.175: E/AndroidRuntime(1362):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-18 19:04:19.175: E/AndroidRuntime(1362):     ... 11 more

1 个答案:

答案 0 :(得分:3)

xml文件的top元素应该是PreferenceScreen而不是Preference

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="pref_sync"
        android:title="@string/pref_sync"
        android:summary="@string/pref_sync_summ"
        android:defaultValue="true" />
    <ListPreference
        android:dependency="pref_sync"
        android:key="pref_syncConnectionType"
        android:title="@string/pref_syncConnectionType"
        android:dialogTitle="@string/pref_syncConnectionType"
        android:entries="@array/pref_syncConnectionTypes_entries"
        android:entryValues="@array/pref_syncConnectionTypes_values"
        android:defaultValue="@string/pref_syncConnectionTypes_default" />
</PreferenceScreen>

从设置documentation

中提取