【发布时间】:2015-05-19 01:27:49
【问题描述】:
我正在尝试使用 android.support.v7.widget.SwitchCompat 在最小 SDK 10 中创建一个开关,但我收到错误 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;
}
}
}
【问题讨论】:
标签: android switchcompat