简体   繁体   中英

App crash when i press the list preference in PreferfenceFragmentCompat

the application crash with the following exception when i press the list preference :-

FATAL EXCEPTION: main
                                                                    Process: co.veemu.populermovies, PID: 25247
                                                                    java.lang.ClassCastException: android.support.v7.preference.PreferenceScreen cannot be cast to android.support.v7.preference.DialogPreference
                                                                        at android.support.v7.preference.PreferenceDialogFragmentCompat.onCreate(PreferenceDialogFragmentCompat.java:89)
                                                                        at android.support.v7.preference.ListPreferenceDialogFragmentCompat.onCreate(ListPreferenceDialogFragmentCompat.java:48)
                                                                        at android.support.v4.app.Fragment.performCreate(Fragment.java:2180)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1244)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                        at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                                        at android.os.Handler.handleCallback(Handler.java:751)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6178)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)

this is what i do for making listpreference :-

i add this gradle dependency

compile 'com.android.support:preference-v7:25.3.1'

2- and make preference file in res/xml folder and this is what file contain

    <ListPreference

    android:title="@string/pref_sort_order_title"
    android:defaultValue="@string/pref_sort_des_value"
    android:entries="@array/pref_sort_entres"
    android:entryValues="@array/pref_sort_entres_value"
    android:summary="sort order"


    />

3- make the activity with layout resource file , the root tag for layout is fragment which pointed to class extended from PreferenceFragmentCompat

4- make the PreferenceFragmentCompat and populate the preference in onCreatePreference()

  @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(R.xml.preference_sort);

}

5- i add the Theme in AppTheme :-

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <!-- this is the preference theme -->
    <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>


</style>

but the application crashes with above exception , i don't have idea what is wrong with code

I know it's a bit late, but recently I faced the same problem so I searched and found the solution.

It's quite an easy fix actually,
Dialog Preference need to have a unique android:key to work properly, so all you have to do is add the android:key attribute to your ListPreference in the XML file

<ListPreference
        android:title="List"
        android:key="list" <!-- Add this to your code -->
        android:entries="@array/entries"
        android:entryValues="@array/values"
        android:defaultValue="Default">
</ListPreference>

Hope this helps and solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM