Error inflating class android.support.design.widge

2020-02-03 04:57发布

I would like use a FloatingActionButton on my application, I read this : https://guides.codepath.com/android/Floating-Action-Buttons#google-s-official-support-library but when I run the Activity I have this error :

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 File

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

}

13条回答
家丑人穷心不美
2楼-- · 2020-02-03 05:37

You have to include the support libraries.

  1. Go to "Project Structure" -> Dependencies
  2. On the right side click "+" and select "1. Library dependency"
  3. Search for "android.support"
  4. Add both:
    • com.android.support:appcompat-v7:.......
    • com.android.support:design:........
  5. Sync Gradle enter image description here Happy codding! :)
查看更多
孤傲高冷的网名
3楼-- · 2020-02-03 05:40

Add these dependencies into your gradle file. This may solve the problem in some cases.

dependencies {
   compile 'com.android.support:appcompat-v7:23.1.1'
   compile 'com.android.support:design:23.1.1'
   ...
}
查看更多
Ridiculous、
4楼-- · 2020-02-03 05:40

You need to add in your app's build gradle the following support library.

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

that was last year, now the latest version is

    compile 'com.android.support:design:27.0.2'
查看更多
相关推荐>>
5楼-- · 2020-02-03 05:42

For me, I came across this error when using a mix of androidx and android.support.v7 libraries.

See my solution for that version of this error here: https://stackoverflow.com/a/52490825/1762493

查看更多
疯言疯语
6楼-- · 2020-02-03 05:45

Just use this line in FloatingActionButton (app instead of android):

app:backgroundTint="@color/colorAccent"
查看更多
干净又极端
7楼-- · 2020-02-03 05:46

Please put compile 'com.android.support:design:23.0.1' inside your project build.gradle file

查看更多
登录 后发表回答