android - 使用 androidx dropdownPreference 的正确方法是什么?

标签 android android-preferences androidx

我正在使用下拉首选项,但每次尝试插入时应用程序都会崩溃。应该提交什么 entryValues 和条目?官方文档没有解释 xml 的使用。在 api 25 和 21 上测试。

<androidx.preference.PreferenceCategory app:title="@string/Time">

    <androidx.preference.DropDownPreference
        android:key="dropdown"
        android:title="@string/waitTime"
        android:entryValues="@array/planets_array"
        android:entries="@array/times"
        app:useSimpleSummaryProvider="true"
        />


</androidx.preference.PreferenceCategory>
</androidx.preference.PreferenceScreen>

字符串.xml:
 <resources>
    <string-array name="planets_array">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
</string-array>

<integer-array name="times">
    <item>5</item>
    <item>10</item>
    <item>15</item>
    <item>20</item>

</integer-array>
</resources>

日志:
Process: com.example.detector, PID: 11439
android.view.InflateException: Binary XML file line #22: Error inflating class androidx.preference.DropDownPreference
    ...
androidx.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:377)
    at com.example.breathdetector.ui.customization.CustomizationFragment.onCreatePreferences(CustomizationFragment.java:24)
 androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
   ...
 Caused by: java.lang.reflect.InvocationTargetException
   ...androidx.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:377) 
    at com.example.breathdetector.ui.customization.CustomizationFragment.onCreatePreferences(CustomizationFragment.java:24) 
    at androidx.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:160) 
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586) 
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838) 
    at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100) 
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874) 

任何帮助,将不胜感激。

最佳答案

看一下样本:

arrays xml

preferences xml

很难从堆栈跟踪中分辨出来,但这可能是因为您使用的是整数数组 - 我认为 DropDownPreference仅适用于条目和条目值的字符串数组。

希望这可以帮助!

关于android - 使用 androidx dropdownPreference 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59139924/

相关文章:

android - 主题对话框首选项

android-recyclerview - 索引越界异常 : Inconsistency detected error while scrolling

android - 如何在 MotionLayout 中运行特定的过渡?

android - 如何使用 MPAndroidChart 制作实时滚动窗口图

android - PreferenceEditText 到字符串

java - Firebase实时数据库: How to use onDisconnect

java - 有没有办法让 SharedPreferences 在我的整个 Android 应用程序中全局化?

android - 在 Android 中使用 butterknife 在 OnEditorAction 中引用空对象

android - Kotlin 多平台项目

android - Android 中的 3D 立方体过渡