Android - 首选项 - ClassCastException

时间:2014-01-19 10:57:16

标签: android sharedpreferences

我测试了一个标准的偏好教程,没有任何问题..但是在本教程中(在所有类似的例子中),首选项活动意图从主活动中开始......

我尝试在我的应用程序中复制简单的测试代码,但是偏好活动意图是从List活动中启动的......(来自菜单栏)

public class TrainingListActivity extends FragmentActivity implements
        TrainingListFragment.Callbacks {

    // .....
    public void settingsActivity() {
        Intent settingsActivity = new Intent(this, Preferences.class);
        startActivity(settingsActivity);
    }
}

所以我将它添加到manifest.xml中,表明TrainingListActivity为父活动

<activity
    android:name="com.swimtechtest.swimmer.TrainingListActivity"
    android:label="@string/app_name" >
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name="com.swimtechtest.swimmer.Preferences"
    android:label="@string/title_preferences"
    android:parentActivityName=".Preferences" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".TrainingListActivity" />
</activity>

但运行时,我收到运行时错误:

  

android.preference.Preference无法强制转换为android.preference.GenericInflater $ Parent

这似乎是在首选项onCreate()方法结束时,在setDefaultValues上提出的......任何线索?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction()
        .replace(android.R.id.content, new PrefsFragment()).commit();
    PreferenceManager.setDefaultValues(Preferences.this, R.xml.preferences,
        false);
}

的preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="My list of Preferences" >
    <CheckBoxPreference
        android:defaultValue="false"
        android:key="checkboxPref"
        android:summary="This preference can be true or false"
        android:title="Checkbox Preference" />
    <ListPreference
        android:defaultValue="digiGreen"
        android:entries="@array/listArray"
        android:entryValues="@array/listValues"
        android:key="listPref"
        android:summary="This preference allows to select an item in a array"
        android:title="Your favorite Pet" />
    <EditTextPreference
        android:name="EditText Preference"
        android:defaultValue="Nothing"
        android:key="editTextPref"
        android:summary="This allows you to enter a string"
        android:title="Edit This Text" />
    <RingtonePreference
        android:name="Ringtone Preference"
        android:key="ringtonePref"
        android:summary="Select a ringtone"
        android:title="Ringtones" />
    <PreferenceScreen
        android:key="SecondPrefScreen"
        android:summary="This is a sub PreferenceScreen"
        android:title="Secondary Level" >
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.preferencesexample.Preferences2"
            android:targetPackage="com.example.preferencesexample" />
    </PreferenceScreen>
    <Preference
        android:key="customPref"
        android:summary="This works almost like a button"
        android:title="Custom Preference" />
</PreferenceCategory>
</PreferenceScreen>

consolelog

FATAL EXCEPTION: main
 Process: com.swimtechtest.swimmer, PID: 9690
 java.lang.RuntimeException: Unable to start activity    
  ComponentInfo{com.swimtechtest.swimmer/com.swimtechtest.swimmer.Preferences}:  
  java.lang.ClassCastException: android.preference.Preference cannot be cast to 
   android.preference.GenericInflater$Parent
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
     ....
     Caused by: java.lang.ClassCastException: android.preference.Preference cannot be cast to android.preference.GenericInflater$Parent
at android.preference.GenericInflater.inflate(GenericInflater.java:320)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:485)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:444)
at com.swimtechtest.swimmer.Preferences.onCreate(Preferences.java:19)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

1 个答案:

答案 0 :(得分:1)

你的意思是:

<activity
    android:name="com.swimtechtest.swimmer.Preferences"
    android:label="@string/title_preferences"
    android:parentActivityName="com.swimtechtest.swimmer.TrainingListActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".TrainingListActivity" />
</activity>