在 Android 中,尝试使用 SwitchCompat 时出现 inflation 错误

in Android, getting inflation error when trying to use SwitchCompat

我正在尝试使用 android.support.v7.widget.SwitchCompat 在最少 10 个 SDK 中创建一个开关,但出现错误 android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v7.widget.SwitchCompat 在寻找解决方案时,我发现它可能有与声明哪种样式有关?所以我将样式更改为 Theme.AppCompat 但我仍然收到无法膨胀的错误。我相当确定我已经添加了正确的支持库(我使用的是 Eclipse Kepler),因为 import android.support.v7.widget.SwitchCompat; 没有错误。所以这是所有相关代码,任何人都可以看到问题出在哪里吗?

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;
    }

}

}

问题是您的 Activity 正在扩展 android.app.Activity,而不是您需要与 AppCompat 库一起使用的支持 Activity。只需更改:

import android.app.Activity;

import android.support.v7.app.ActionBarActivity;

或者,因为我认为在 API 22 中已弃用:

import android.support.v7.app.AppCompatActivity;

一旦你改变了那些它应该工作。