我在PreferfenceFragmentCompat中按下列表首选项时应用程序崩溃

时间:2017-07-04 20:56:31

标签: android sharedpreferences

当我按下列表首选项时,应用程序崩溃并出现以下异常: -

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)

这是我为制作列表首选项所做的: -

我添加了这个gradle依赖

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

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

4-制作PreferenceFragmentCompat并在onCreatePreference()中填充首选项

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

}

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>

但应用程序因上述异常崩溃,我不知道代码有什么问题

1 个答案:

答案 0 :(得分:1)

我知道它有点晚了,但最近我遇到了同样的问题所以我搜索并找到了解决方案。

实际上这很容易解决,
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>

希望这有助于解决您的问题。