1

每次我尝试使用这个偏好类时,我都会得到一个 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
    }
}

偏好.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
4

1 回答 1

3

xml 文件的顶部元素应该是 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>

从设置文档中提取

于 2012-11-19T00:26:15.553 回答