Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.view.InflateException: Binary XML file line #4: Error inflating class SwitchPreference

Tags:

java

android

I'm trying to read my app preferences and I get this error:

Settings activity:

public class Settings extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        try
        {
            addPreferencesFromResource(R.xml.prefs);
        }
        catch (Exception ex)
        {
            Log.e("errorSettings", Log.getStackTraceString(ex));
        }
    }
}

Preferences XML File:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="General">
    <SwitchPreference
        android:title="Downloader"
        android:defaultValue="true"
        android:key="useDownloader"
        android:summary="Enable to use" />
    </PreferenceCategory>

</PreferenceScreen>

and on the application manifest I set this:

<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="15" />

and the first error I get is:

android.view.InflateException: Binary XML file line #4: Error inflating class SwitchPreference

Thanx upfront.

like image 324
Eli Revah Avatar asked Dec 08 '22 23:12

Eli Revah


1 Answers

SwitchPreference was added in API Level 14. You cannot use it on earlier versions of Android. Since you are trying to support back to API Level 9, you can either:

  1. Use different preference XML for earlier versions, using a CheckBoxPreference instead of a SwitchPreference, or

  2. Just use CheckBoxPreference and drop SwitchPreference for now

like image 109
CommonsWare Avatar answered May 22 '23 04:05

CommonsWare