无法让 android.support.v14.preference.MultiSelectListPreference 工作

Can't get android.support.v14.preference.MultiSelectListPreference to work

不知道哪里出了问题。错误日志没有帮助,我也尝试了这里提到的一些解决方案。

05-24 17:32:05.674 2283-2283/? E/Crash: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xyz.android/com.xyz.android.activities.user_management.NewUserProfileActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class android.support.v14.preference.MultiSelectListPreference
                                        -------------------------------
                                        --------- Stack trace ---------
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
android.app.ActivityThread.wrap11(ActivityThread.java)android.app.ActivityThread$H.handeMessage(ActivityThread.java:1494)
android.os.Handler.dispatchMessage(Handler.java:111)
android.os.Looper.loop(Looper.java:207)
android.app.ActivityThread.main(ActivityThread.java:5770)java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
--------- Cause ---------
android.view.InflateException: Binary XML file line #20: Error inflating class android.support.v14.preference.MultiSelectListPreference
android.support.v7.preference.PreferenceInflater.createItem(PreferenceInflater.java:263
android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:292)
android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:359)
android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:361)
android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:167)
android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:117)
android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:128)
android.support.v7.preference.PreferenceFragmentCompat.addPreferencesFromResource(PreferenceFragmentCompat.java:428)
com.xyz.android.activities.user_management.UserProfilePrefFragment.onCreatePreferences(UserProfilePrefFragment.java:12)
android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:223)
android.support.v4.app.Fragment.performCreate(Fragment.java:2180)
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1244)
android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1085)
android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:976)
android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95)
android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146
android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)                                 

这里是首选项

<android.support.v14.preference.MultiSelectListPreference
        android:defaultValue="array/pref_hobby_list_default"
        android:dialogTitle="Hobbies"
        android:summary="set your hobbies"
        android:key="hobby_pref"
        android:title="Hobbies"
        android:icon="@drawable/add_favorite"
        android:entries="@array/hobbies"
        android:entryValues="@array/hobby_values"/>

对应的字符串数组

    <string-array name="hobbies">
    <item>Football</item>
    <item>Travelling</item>
    <item>Listening to Music</item>
    <item>Painting</item>
    <item>Watching Movies</item>
    <item>Shopping</item>
    <item>Meeting new people</item>
</string-array>

<string-array name="hobby_values">
    <item>football</item>
    <item>travelling</item>
    <item>listening_to_music</item>
    <item>painting</item>
    <item>watching_movies</item>
    <item>shopping</item>
    <item>meeting_new_people</item>
</string-array>

<string-array name="pref_hobby_list_default">
    <item>football</item>
    <item>travelling</item>
    <item>listening_to_music</item>
    <item>painting</item>
    <item>watching_movies</item>
    <item>shopping</item>
    <item>meeting_new_people</item>
</string-array>

偏好片段:

import android.support.v7.preference.PreferenceFragmentCompat;

public class UserProfilePrefFragment extends PreferenceFragmentCompat {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    // Load the Preferences from the XML file
    addPreferencesFromResource(R.xml.app_preferences);
}

}

您忘记了 @ 登录 android:defaultValue="array/pref_hobby_list_default"