扩展类android.support.design.widget.CoordinatorLayout时出错

问题描述 投票:33回答:13

我想在我的应用程序上使用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>

Graddle

*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.+'

}
android floating-action-button
13个回答
40
投票

如果您使用Activity,请将其更改为AppCompatActivity。它可能是活动时的错误。


1
投票

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

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

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

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

0
投票

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

请在此处查看我的解决方案,了解此错误的版本:https://stackoverflow.com/a/52490825/1762493


0
投票

适用于使用AndroidX Dependency的用户。在你的xml文件中确保所有你的

 android.support.???

CoordinatorLayout中的节点(如包含的Action Bars)也替换为

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

那些 。


-1
投票

它对我有用

禁用即时运行

File => Settings => Build,Execution,Deployment => Instant Run


27
投票

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

app:backgroundTint="@color/colorAccent"

18
投票

请将compile 'com.android.support:design:23.0.1'放在项目build.gradle文件中


16
投票

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

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

或任何其他Theme.AppCompat *


12
投票

适用于使用AndroidX Dependency的用户

除了更改依赖项外,还必须更改XML。

<android.support.design.widget.CoordinatorLayout 

<androidx.coordinatorlayout.widget.CoordinatorLayout

6
投票

您必须包含支持库。

  1. 转到“项目结构” - >依赖项
  2. 在右侧单击“+”并选择“1.库依赖”
  3. 搜索“android.support”
  4. 添加两个: com.android.support:appcompat-v7:....... com.android.support:design:........
  5. Sync Gradle enter image description here快乐编码! :)

4
投票

需要两个:

  • 扩展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>

3
投票

For Xamarin Developers:

(支持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. 您的应用活动应来自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>

3
投票

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

dependencies {
   compile 'com.android.support:appcompat-v7:23.1.1'
   compile 'com.android.support:design:23.1.1'
   ...
}
© www.soinside.com 2019 - 2024. All rights reserved.