Search code examples
androidswitchcompat

in Android, getting inflation error when trying to use SwitchCompat


I'm trying to use android.support.v7.widget.SwitchCompat to create a switch in minimum SDK of 10 and I'm getting the error android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v7.widget.SwitchCompat In looking for solutions, I've found that it may have something to do with which style is declared? So I've changed the style to Theme.AppCompat but I'm still getting the error that it can't inflate. I'm fairly certain that I've got the correct support library added (I'm using Eclipse Kepler) because there are no errors at import android.support.v7.widget.SwitchCompat;. So here's all the relevant code, can anyone see where the problem is?

Manifest:
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Splash"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
....


Style:
<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

-->
<style name="AppBaseTheme" parent="Theme.AppCompat">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.

    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
...
</resources>



XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="horizontal" >

<android.support.v7.widget.SwitchCompat
    android:id="@+id/test1_SW"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="50dp"
    android:layout_marginStart="50dp"
    android:layout_marginTop="25dp"
    android:checked="false"
    android:text="SwitchCompat"
    android:textOff="OFF"
    android:textOn="ON" />

</RelativeLayout>


Activity:
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.widget.CompoundButton;

public class SwitchTest extends Activity implements
    CompoundButton.OnCheckedChangeListener {
private SwitchCompat test1_SW;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.cutin, R.anim.cutout);
    setContentView(R.layout.switch_test);

    // --- one
    test1_SW = (SwitchCompat) findViewById(R.id.test1_SW);
    test1_SW.setSwitchPadding(40);
    test1_SW.setOnCheckedChangeListener(this);
    // --- END one

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()) {
    case R.id.test1_SW:
        Log.i("test1_SW", isChecked + "");
        break;
    }

}

}

Solution

  • The problem is your Activity is extending android.app.Activity, and not the support Activity you need to use with the AppCompat libraries. Just change:

    import android.app.Activity;
    

    to

    import android.support.v7.app.ActionBarActivity;
    

    Or, since I think that's been deprecated in API 22:

    import android.support.v7.app.AppCompatActivity;
    

    Once you change those it should work.