错误膨胀 class android.support.design.widget.CoordinatorLayout

Error inflating class android.support.design.widget.CoordinatorLayout

我想在我的应用程序上使用 FloatingActionButton,我读到这个:https://guides.codepath.com/android/Floating-Action-Buttons#google-s-official-support-library 但是当我 运行 Activity 我有这个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xavier_laffargue.podcast/com.xavier_laffargue.podcast.ACT_Test}: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.design.widget.CoordinatorLayout

XML 文件

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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">

    <ListView
        android:id="@+id/lvToDoList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="24dp"
        android:src="@drawable/ic_action_refresh"
        app:layout_anchor="@id/lvToDoList"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

摇篮

*apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.xavier_laffargue.podcast"
        minSdkVersion 21
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:design:22.2.0'

    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:support-v13:22.2.0'
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'

}

如果您使用Activity,请将其更改为AppCompatActivity。可能是 Activity.

时就变成了错误

我有同样的错误。只需将项目父主题更改为

<style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

或任何其他 Theme.AppCompat*

两者都需要:

  • 扩展 AppCompatActivity 而不是 FragmentActivity

    public class MyActivity extends AppCompatActivity
    
  • 所用样式的父级 (/res/values/styles.xml)

    <style name="MyStyle" parent="Theme.AppCompat">
    

另外:

  • 在 styles.xml

    中定义颜色
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    

请将 compile 'com.android.support:design:23.0.1' 放入您的项目 build.gradle 文件

只需在 FloatingActionButton 中使用这一行(app 而不是 android):

app:backgroundTint="@color/colorAccent"

将这些依赖项添加到您的 gradle 文件中。这在某些情况下可能会解决问题。

dependencies {
   compile 'com.android.support:appcompat-v7:23.1.1'
   compile 'com.android.support:design:23.1.1'
   ...
}

您必须包含支持库。

  1. 转到 "Project Structure" -> 依赖项
  2. 点击右边的“+”和select“1.库依赖”
  3. 搜索 "android.support"
  4. 同时添加:
    • com.android.support:appcompat-v7:.......
    • com.android.support:设计:.......
  5. 同步Gradle 编码快乐! :)

对于 Xamarin 开发人员:

(支持Android API 7至22+)

  1. 确保您已经安装了以下组件:
    • Android 支持设计库
    • Android 支持库 v7 AppCompat
  2. 确保安装并引用了这些组件的所有 NuGet 包。这些都是:
    • Xamarin.Android.Support.Design
    • Xamarin.Android.Support.v4
    • Xamarin.Android.Support.v7.AppCompat
    • Xamarin.Android.Support.v7.RecyclerView
  3. 您的应用 Activity 应派生自 Android.Support.V7.App.AppCompatActivity
  4. 您使用的样式必须源自 Theme.AppCompat.* 样式。所以你的 Resources\values\styles.xml 应该是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="YourAppTheme" parent="Theme.AppCompat">
            <item name="colorPrimaryDark">#AB000D</item>
            <item name="colorPrimary">#E53935</item>
            <item name="colorAccent">#00B8D4</item>
        </style>
        <!-- other styles... -->
    </resources>
    
  5. 还要确保您确实在 AndroidManifest.xml

    中使用了主题
    <application android:theme="@style/YourAppTheme"></application>
    

对我有用

禁用即时 运行

文件 => 设置 => 构建、执行、部署 => 即时 运行

您需要在应用的构建中添加 gradle 以下支持库。

    compile 'com.android.support:design:23.0.1'

那是去年的,现在最新的版本是

    compile 'com.android.support:design:27.0.2'

对我来说,我在混合使用 androidxandroid.support.v7 库时遇到了这个错误。

在此处查看我针对此错误版本的解决方案:

对于那些使用 AndroidX 依赖关系的人

随着依赖关系的改变,XML也必须改变。

来自

<android.support.design.widget.CoordinatorLayout 

<androidx.coordinatorlayout.widget.CoordinatorLayout

对于那些使用 AndroidX 依赖项的人。 在你的 xml 文件中确保你所有的

 android.support.???

CoordinatorLayout 中的节点 (就像包含的 ActionBars) 也被替换为

androidx.??? (or com.google.android.???)

个 .