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

Preferences.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ファイルの最上位要素はPreferenceではなくPreferenceScreenである必要があります

<?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 に答える