Home > Enterprise >  Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton
Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton

Time:12-20

I am getting an "Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton" when i run my application on my phone. I know there are other posts with this but its not the same or al least the answers there do not work for me.

So when i run my application on the emlulator all works great. Not error's or anything. When i run the same application on my phone (with the same android API version) i get the below error.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tripmemory, PID: 25604
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tripmemory/com.tripmemory.TripActivity}: android.view.InflateException: Binary XML file line #21 in com.tripmemory:layout/activity_trip: Binary XML file line #21 in com.tripmemory:layout/activity_trip: Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4166)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4312)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2571)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8741)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
 Caused by: android.view.InflateException: Binary XML file line #21 in com.tripmemory:layout/activity_trip: Binary XML file line #21 in com.tripmemory:layout/activity_trip: Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton
 Caused by: android.view.InflateException: Binary XML file line #21 in com.tripmemory:layout/activity_trip: Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at android.view.LayoutInflater.createView(LayoutInflater.java:876)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1028)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:983)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1145)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1106)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:692)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:544)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:491)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:710)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
    at com.tripmemory.TripActivity.onCreate(TripActivity.java:44)
    at android.app.Activity.performCreate(Activity.java:8578)
    at android.app.Activity.performCreate(Activity.java:8557)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4147)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4312)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2571)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8741)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) 
E/AndroidRuntime: Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 77: TypedValue{t=0x2/d=0x7f0300e0 a=-1}, theme={InheritanceMap=[id=0x7f11024dcom.tripmemory:style/Theme.TripMemory, id=0x7f110225com.tripmemory:style/Theme.MaterialComponents.DayNight.DarkActionBar, id=0x7f11021ecom.tripmemory:style/Theme.MaterialComponents, id=0x7f11005acom.tripmemory:style/Base.Theme.MaterialComponents, id=0x7f110096com.tripmemory:style/Base.V21.Theme.MaterialComponents, id=0x7f110083com.tripmemory:style/Base.V14.Theme.MaterialComponents, id=0x7f110084com.tripmemory:style/Base.V14.Theme.MaterialComponents.Bridge, id=0x7f110126com.tripmemory:style/Platform.MaterialComponents, id=0x7f1101eacom.tripmemory:style/Theme.AppCompat, id=0x7f110046com.tripmemory:style/Base.Theme.AppCompat, id=0x7f1100a8com.tripmemory:style/Base.V28.Theme.AppCompat, id=0x7f1100a5com.tripmemory:style/Base.V26.Theme.AppCompat, id=0x7f11009fcom.tripmemory:style/Base.V23.Theme.AppCompat, id=0x7f11009dcom.tripmemory:style/Base.V22.Theme.AppCompat, id=0x7f110092com.tripmemory:style/Base.V21.Theme.AppCompat, id=0x7f1100aacom.tripmemory:style/Base.V7.Theme.AppCompat, id=0x7f110124com.tripmemory:style/Platform.AppCompat, id=0x7f11012fcom.tripmemory:style/Platform.V25.AppCompat, id=0x103022eandroid:style/Theme.Material.NoActionBar, id=0x1030224android:style/Theme.Material, id=0x1030005android:style/Theme], Themes=[com.tripmemory:style/Theme.TripMemory, forced, com.tripmemory:style/Theme.AppCompat.Empty, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
    at android.content.res.TypedArray.getColorStateList(TypedArray.java:600)
    at android.view.View.<init>(View.java:6211)
    at android.widget.ImageView.<init>(ImageView.java:196)
    at android.widget.ImageButton.<init>(ImageButton.java:96)
    at android.widget.ImageButton.<init>(ImageButton.java:92)
    at com.google.android.material.internal.VisibilityAwareImageButton.<init>(VisibilityAwareImageButton.java:47)
    at com.google.android.material.floatingactionbutton.FloatingActionButton.<init>(FloatingActionButton.java:207)
    at com.google.android.material.floatingactionbutton.FloatingActionButton.<init>(FloatingActionButton.java:201)
        ... 29 more

in other posts i have read the theme is wrong and they advice to change it to a sub of materialComponents but that is already the case for me:

Theme.MaterialComponents.DayNight.DarkActionBar

The i read somebody changing the background but i do not do that:

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@ id/floatingActionButton"
    app:srcCompat="@drawable/plus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/margin_M"
    android:backgroundTint="?attr/colorAccent1"
    android:clickable="true"/>

In short i am lost as to the root cause specially because it works fine in the emulator. Can anybody help me with this?

CodePudding user response:

If you look lower in the stack trace, you will see this line:

E/AndroidRuntime: Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 77: TypedValue{t=0x2/d=0x7f0300e0 a=-1} ...

There is an attribute that cannot be resolved. The id is `0x7f0300e01 and, since the error points to the floating action button, the missing attribute will be referenced there.

This looks like an issue with themes. I would look at

android:layout_margin="@dimen/margin_M"
android:backgroundTint="?attr/colorAccent1"

and removed each one-by-one to isolate the problem and go from there. (This is just to isolate the problem. I am not suggesting that you don't need these values.)

Update: Actually since the following line appears in the trace

at android.content.res.TypedArray.getColorStateList(TypedArray.java:600)

I think the issue is with ?attr/colorAccent1. Check your theme for the presence of this attribute. Also, make sure that this attribute is defined as a color.

  • Related