Error inflating class com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton

Issue

Although there is a similar question, it does not give me a solution. When I’m trying to use com.google.android.material.floatingactionbutton.FloatingActionButton it runs well. But if I try to use com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton, it gives errors.

My code in the XML file:

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    android:id="@+id/book_profile_write_review"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:contentDescription="@string/author_name"
    android:text="@string/book_name"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/ic_launcher_foreground" />

logcat:

2019-11-14 12:30:21.997 21326-21326/com.bookaholic.shahad.bookaholicbd E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bookaholic.shahad.bookaholicbd, PID: 21326
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bookaholic.shahad.bookaholicbd/com.bookaholic.shahad.bookaholicbd.activities.BookProfile}: android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2974)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3059)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:7000)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
 Caused by: android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
 Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
    at android.view.LayoutInflater.createView(LayoutInflater.java:647)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
    at com.bookaholic.shahad.bookaholicbd.activities.BookProfile.onCreate(BookProfile.java:20)
    at android.app.Activity.performCreate(Activity.java:7258)
    at android.app.Activity.performCreate(Activity.java:7249)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1222)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3059)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:7000)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
 Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
    at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
    at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)
    at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)
    at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:76)
    at com.google.android.material.button.MaterialButton.<init>(MaterialButton.java:200)

Can you please help me?

Solution

From your logcat

Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

So add a material theme for your app/current activity will fix this issue

1) add dependency* (latest / more stable version)

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

2) Create a new theme

<style name="MyMaterialTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Add attributes here -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="textAppearanceButton">@style/AppTextAppearance.Button</item>
</style>
 <!--To fix rendering in preview -->
 <style name="AppTextAppearance.Button" parent="TextAppearance.MaterialComponents.Button">
    <item name="android:textAllCaps">true</item>
</style>

change parent material theme according to you parent=”Theme.MaterialComponents.*

3) change app theme or current activity theme in manifest

android:theme="@style/MyMaterialTheme"

or

<activity android:name=".MyActivity"
android:theme="@style/MyMaterialTheme"> </activity>

4) If you found any rendering issue in previews, adding themes to ExtendedFloatingActionButton will fix this

Failed to find '@attr/textAppearanceButton' in current theme.

add theme to ExtendedFloatingActionButton android:theme=”@style/MyMaterialTheme”

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:theme="@style/MyMaterialTheme"
        android:text="ExtendedFab"            
        app:icon="@drawable/ic_add_a_photo_white_24dp"
       />

For more info visit Material Design Page

Answered By – Adarsh Vijayan P

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published