Error inflating class android.support.design.widget.FloatingActionButton in Android

我怕爱的太早我们不能终老 提交于 2019-11-28 12:43:24

In your Case, Problem is in your styles.xml, your base theme name is AppBaseTheme. Change it to AppTheme as you can see you have used AppTheme in your Mainfest file.

I have listed all the reasons that may cause class android.support.design.widget.FloatingActionButton. Please read the points carefully.

  1. FAB button is Design Library Cpmponent. Your Activity must extend AppCompatActivity instead of Activity.

  2. Your activity theme also should be appropriate Theme.AppCompat theme. Also FAB requires colorAccent. Make sure you have them inside your styles.xml.

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.AppTheme">
    <!-- Customize your theme here. -->
    </style>
    
    <style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <item name="android:colorAccent">@color/accent</item>
    </style>
    
  3. Use app:backgroundTint="@color/your_color" instead of android:backgroundTint.

  4. Don't miss out on adding design library inside your project build.gradle file

    dependencies {
     compile 'com.android.support:appcompat-v7:23.1.1'
     compile 'com.android.support:design:23.1.1'
     ...
    }
    
  5. Update Support Library on SDK Manager in Extras > Android Support Repository and Android Support Library

Hope it solves your error.

If you are using backgroundTint on the API 21+ then just use this line in FloatingActionButton (app instead of android):

app:backgroundTint="@color/colorAccent"

Here is the sample layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:elevation="6dp"
        android:tint="#FFF"
        app:srcCompat="@drawable/ic_add_black_24dp" />

</android.support.design.widget.CoordinatorLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!