Vanya Rachel
Vanya Rachel

Reputation: 1403

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

My app crashed because

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

This is my code in the XML

<android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/icon_right"
        app:backgroundTint="@color/green"
        android:onClick="previewphoto"
        app:layout_anchorGravity="bottom|right|end" />

and here is my gradle

compile 'com.android.support:appcompat-v7:23.4.0' // appcompat library
compile 'com.android.support:design:23.4.0'

my logcat

FATAL EXCEPTION: main Process: com.cyanlabsid.cetakphoto, PID: 15298 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cyanlabsid.cetakphoto/com.cyanlabsid.cetakphoto.PhotoPicker}: android.view.InflateException: Binary XML file line #76: Binary XML file line #76: Error inflating class android.support.design.widget.FloatingActionButton at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) at android.app.ActivityThread.access$900(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5441) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) Caused by: android.view.InflateException: Binary XML file line #76: Binary XML file line #76: Error inflating class android.support.design.widget.FloatingActionButton at android.view.LayoutInflater.inflate(LayoutInflater.java:543) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at com.cyanlabsid.cetakphoto.PhotoPicker.onCreate(PhotoPicker.java:74) at android.app.Activity.performCreate(Activity.java:6303) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)  at android.app.ActivityThread.access$900(ActivityThread.java:153)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5441)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

Can somebody tell my fault?

Upvotes: 39

Views: 41962

Answers (12)

Android
Android

Reputation: 2393

Add in

build.gradle(:app)

 implementation 'com.google.android.material:material:1.2.0'

Use this in your XML

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/icon_right"
        app:backgroundTint="@color/green"
        android:onClick="previewphoto"
        app:layout_anchorGravity="bottom|right|end" />

Upvotes: 1

blackers
blackers

Reputation: 193

I was using Material Design for my ExtendedFloatingActionButton and when using API 24 it would crash the app.

I tried updating my gradle dependencies to the latest version:

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0-alpha02'

but I had no luck with that either. My solution was adding android:theme="" even though I had set a style it would still crash so I set the theme to the same as the style and it worked perfectly.

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
                    android:id="@+id/btn_one"
                    android:theme="@style/"
                    style="@style/"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:contentDescription="@string/text"
                    android:text="@string/text"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

Upvotes: 1

Nikhil Ranjan
Nikhil Ranjan

Reputation: 56

 <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@drawable/ic_add_foreground"
        android:layout_margin="16dp" />

Use this

 <com.google.android.material.floatingactionbutton.FloatingActionButton>

instead of

<android.support.design.widget.FloatingActionButton>

Upvotes: 2

Rachid Rajjys
Rachid Rajjys

Reputation: 144

Make sure the library from which you declared the FloatingActionButton in the activity is the same as the one used for the layout markup

For example have

import com.google.android.material.floatingactionbutton.FloatingActionButton; in the activity and com.google.android.material.floatingactionbutton.FloatingActionButton as the tag for the layout

Upvotes: 1

user330844
user330844

Reputation: 880

I am using:

classpath 'com.android.tools.build:gradle:3.3.2'

and

distributionUrl=https://services.gradle.org/distributions/gradle-4.10.1-all.zip

I changed my XML to:

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:focusable="true"
        app:srcCompat="@drawable/mapit"
        app:layout_anchor="@id/foundit_imageView"
        app:layout_anchorGravity="bottom|right|end" />

and everything built and ran correctly.

I hope this helps

Upvotes: 5

Muhammad Aamir
Muhammad Aamir

Reputation: 131

You need to add

android:theme="@style/Theme.AppCompat"

in the XML_Layout file in which you are using FloatingActionButton...

Upvotes: 13

Pradeep Sheoran
Pradeep Sheoran

Reputation: 493

use this code it will be work better ;

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="16dp"
        app:backgroundTint="#f9fcfc"

        android:src="@drawable/chat_float"
        tools:targetApi="lollipop" />

Upvotes: 0

Dus
Dus

Reputation: 4230

Tried your code, the problem is with the 23.4.0 library.

Upgrade to 24.+ , there's no error in that lib.

For example :

implementation 'com.android.support:appcompat-v7:24.2.1' // appcompat library
implementation 'com.android.support:design:24.2.1'

You need to change the compile version to 24 too.

compileSdkVersion 24

Upvotes: 14

Thien Nguyen Minh
Thien Nguyen Minh

Reputation: 321

Because android:backgroundTint doesn't work below android API 21, so you need to use app:backgroundTint instead.

Upvotes: 22

Juboraj Sarker
Juboraj Sarker

Reputation: 957

Use

app:backgroundTint

in stead of

android:backgroundTint

Hope it will work.

Upvotes: 57

Ashana.Jackol
Ashana.Jackol

Reputation: 3134

Below API level 21 app versions not support for the

app:backgroundTint="@color/green"

or you can use this library for achieve more material design widgets.

https://github.com/navasmdc/MaterialDesignLibrary

happy coding...

Upvotes: 14

Ravee Sunshine
Ravee Sunshine

Reputation: 386

I had the same problem

I faced the same issue with Pre-Lolipop version and to resolve it, I just changed "android:src" to "app:srcCompat" & it worked for me.

To make compatibility with older version and if you're using a Vector graphics (as drawable assets) you should use:

app:srcCompat="@drawable/you_graphics"

instead of:

android:src="@drawable/your_graphics"

Upvotes: 10

Related Questions