1

我正在尝试使用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;
    }

}

}
4

1 回答 1

2

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

import android.app.Activity;

import android.support.v7.app.ActionBarActivity;

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

import android.support.v7.app.AppCompatActivity;

一旦你改变了这些,它应该可以工作。

于 2015-05-18T17:27:49.767 回答