简体   繁体   English

当我按PreferfenceFragmentCompat中的列表首选项时,应用崩溃

[英]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 :- 这就是我做的listpreference:-

i add this gradle dependency 我添加了这个gradle依赖

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

2- and make preference file in res/xml folder and this is what file contain 2-并在res / xml文件夹中创建首选项文件,这是包含的文件

    <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 3-使用布局资源文件进行活动,布局的根标记是片段,该片段指向从PreferenceFragmentCompat扩展的类

4- make the PreferenceFragmentCompat and populate the preference in onCreatePreference() 4-制作PreferenceFragmentCompat并在onCreatePreference()中填充首选项

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

}

5- i add the Theme in AppTheme :- 5-我在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 Dialog Preference需要具有唯一的android:key才能正常工作,因此您要做的就是将android:key属性添加到XML文件中的ListPreference中。

<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. 希望这可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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