Rifat
Rifat

Reputation: 1888

What is the correct way to use androidx dropdownPreference?

I'm using dropdown preference but app crashes each time it tries to infalte. What should be entryValues and entries filed? official doc does not explain about xml usage. Tested on api 25 and 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>

string.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>

Log:

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) 

any help would be appreciated.

Upvotes: 4

Views: 1671

Answers (1)

Louis
Louis

Reputation: 514

Take a look at the sample:

arrays xml

preferences xml

Hard to tell from the stacktrace, but it might be because you are using an integer array - I think DropDownPreference only works with string arrays for both entries and entry values.

Hope this helps!

Upvotes: 4

Related Questions