I've made a simple PreferenceScreen
with multiple Preference Categorys
and CheckBoxPreferences
.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="General">
<CheckBoxPreference
android:title="GPS"
android:key="gps_preference"
android:summary="Enales the GPS tracking of your device"
android:defaultValue="true"
></CheckBoxPreference>
<SwitchPreference
android:title="WLAN"
android:key="wlan_preference"
android:summary="Enables the WLAN usage"
android:defaultValue="true"></SwitchPreference>
<CheckBoxPreference
android:title="Benachrichtigungen"
android:key="benachrichtigung_preference"
android:summary="Enables the Push messages"
android:defaultValue="true"
></CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
This Preference screen is inside a android.support.v7.preference.PreferenceFragmentCompat
. If I leave everyhing like it is, everything works just fine.
However if i want to switch out one CheckBoxPreference
for a SwitchPreference
(which should work by simply switching out the xml tag for CheckBoxPreference
with SwitchPreference
) I'm getting a rendering error in my design
tab:
Exception raised during rendering: android.preference.CheckBoxPreference cannot be cast to android.preference.GenericInflater$Parent
Stack Trace:
android.view.InflateException: Binary XML file line #15: Error inflating class (not found)SwitchPreference
at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:301)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:359)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:361)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:167)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:117)
at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:115)
at android.support.v7.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:424)
at com.julianriegraf.app2night.SettingsFragment.onCreatePreferences(SettingsFragment.java:22)
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:216)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2062)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051)
at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:795)
at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:835)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:676)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.preference.SwitchPreference" on path: DexPathList[[zip file "/data/app/com.julianriegraf.app2night-2/base.apk"],nativeLibraryDirectories=[/data/app/com.julianriegraf.app2night-2/lib/x86, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.support.v7.preference.PreferenceInflater.createItem(PreferenceInflater.java:233)
at android.support.v7.preference.PreferenceInflater.onCreateItem(PreferenceInflater.java:281)
at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:290)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:359)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:361)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:167)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:117)
at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:115)
at android.support.v7.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:424)
at com.julianriegraf.app2night.SettingsFragment.onCreatePreferences(SettingsFragment.java:22)
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:216)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2062)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051)
at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:795)
at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:835)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:676)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Use PreferenceFragmentCompat to programatically handle preferences. To load the settings into the fragment, load the preferences in the onCreatePreferences() method. To get an instance of a preference, search for a preference using its key through the PreferenceManager within onCreateView() .
androidx.preference.PreferenceScreen. A top-level container that represents a settings screen. This is the root component of your Preference hierarchy. A PreferenceFragmentCompat points to an instance of this class to show the preferences.
Shows a hierarchy of Preference objects as lists. These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences that the preference hierarchy in this fragment will use, call PreferenceManager#getDefaultSharedPreferences(android.
It's still possible to customise the appearance of a Preference item though. In your XML you have to declare the root element as android:id="@android:id/widget_frame , and then declare TextView as android:title and android:summary . You can then declare other elements you want to appear in the layout.
There is no class named android.support.v7.preference.SwitchPreference
. There is android.support.v7.preference.SwitchPreferenceCompat
, android.support.v14.preference.SwitchPreference
, and the native android.preference.SwitchPreference
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With