【问题标题】:in Android, getting inflation error when trying to use SwitchCompat在 Android 中,尝试使用 SwitchCompat 时出现通货膨胀错误
【发布时间】: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


    【解决方案1】:

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

    import android.app.Activity;
    

    import android.support.v7.app.ActionBarActivity;
    

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

    import android.support.v7.app.AppCompatActivity;
    

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

    【讨论】:

    • 我认为你在正确的轨道上,我已将 Activity 更改为 AppCompatActivity,但现在我收到错误 java.lang.NoSuchFieldError: android.support.v7.appcompat.R$styleable.Theme_windowActionBar
    • 当我用谷歌搜索该错误时,人们建议备份到库的 v20。这没有帮助,更多的错误,所以现在我回到 v21
    • 非常确定您需要确保您的应用在 styles.xml 中使用的主题也使用 AppCompat 主题,而不是默认主题。我还建议使用您的 SDKManager 下载的最新版本的支持库,并确保您在应用程序引用的项目中确实拥有最新的资源文件,否则您可能会遇到错误(您需要更多不仅仅是罐子)
    • 我通过从我的工作区中删除支持库 appcompat_v7 并从 sdk 位置重新导入它来修复它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    相关资源
    最近更新 更多