Preferences Support Library – SwitchPreference not working

A few days ago Google introduced the Preference Support Library (Link). I’ve just tried to implement it in my application though it seems like it’s not working with SwitchPreferences which is weird because Google states that we can use the same XML-Files as before and explicity says that SwitchPreferences are now available to all API 7+ devices.

Quote (Source)

  • Developing a shared Android / iOS network library
  • Picasso java.lang.IllegalStateException: Method call should not happen from the main thread
  • Android App is crashing when installing
  • Cocos2d-x android can't resolved coscos2dx library
  • Tinting Checkbox on pre v21
  • Compile, Provided, APK - Android dependency scope
  • […] and add preferences using the same preference XML files
    (http://goo.gl/wOcIxI), while adding support for elements such as
    SwitchPreference (previously only available on API 14+ devices) to all
    API 7+ devices. […]

    Error message

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.package/com.my.package.Main}: android.view.InflateException: Binary XML file line #4: Error inflating class (not found)SwitchPreference
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5254)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    

    Relevant part is obviously Binary XML file line #4: Error inflating class (not found)SwitchPreference.

    Java code

    public class FragmentSettings extends PreferenceFragmentCompat {
    
        @Override
        public void onCreatePreferences(Bundle bundle, String s) {
            addPreferencesFromResource(R.xml.preferences);
        }
    }
    

    preferences.xml

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <SwitchPreference
            android:key="pref_something"
            android:summary="Lorem ipsum dolor sit amet"
            android:title="Lorem ipsum" />
    
    </PreferenceScreen>
    

    So basically the question is: Am I doing something wrong or is this indeed a bug of the Preference Support Library?

    Related posts:

    How to use assert in android?
    Difference between getString() and getResources.getString()
    Save File using Binary String on Android via HTML5
    how to get current location in google map android
    android N failing to compile in app billing AIDL
    SurfaceView in Layout
  • Importing CardView and RecyclerView (Android 5.0) in my existing project (eclipse)
  • Playing video from http server in android
  • What is the “Android Class Name” for a Facebook app written in Flex?
  • What is the difference between a dialog being dismissed or canceled in Android?
  • Android Media Player: Download to File and Stream From File
  • Android Studio & ProGuard: cannot resolve symbol getDefaultProguardFile?
  • One Solution collect form web for “Preferences Support Library – SwitchPreference not working”

    SwitchPreference added in API level 14. if you are using Preferences Support Library v7, you must use SwitchPreferenceCompat instead.

    <SwitchPreferenceCompat
        android:key="pref_something"
        android:summary="Lorem ipsum dolor sit amet"
        android:title="Lorem ipsum" />
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.