InflateException with FloatingActionButton from Official Design Library
S

7

88

I am getting a bug using the official FloatingActionButton from Google's support design library.

Here is my LogCat.

android.view.InflateException: Binary XML file line #34: Error inflating class android.support.design.widget.FloatingActionButton
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:655)
at android.view.LayoutInflater.inflate(Unknown Source)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at ---.---.com.---.SubCategoryFragment.onCreateView(SubCategoryFragment.java:47)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
... 24 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable()' on a null object reference
at android.graphics.drawable.LayerDrawable$ChildDrawable.<init>(LayerDrawable.java:968)
at android.graphics.drawable.LayerDrawable$LayerState.<init>(LayerDrawable.java:1014)
at android.graphics.drawable.RippleDrawable$RippleState.<init>(RippleDrawable.java:910)
at android.graphics.drawable.RippleDrawable.createConstantState(RippleDrawable.java:901)
at android.graphics.drawable.RippleDrawable.createConstantState(RippleDrawable.java:90)
at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:932)
at android.graphics.drawable.RippleDrawable.mutate(RippleDrawable.java:891)
at android.view.View.applyBackgroundTint(View.java:16324)
at android.view.View.setBackgroundDrawable(View.java:16193)
at android.support.design.widget.FloatingActionButton.access$201(FloatingActionButton.java:56)
at android.support.design.widget.FloatingActionButton$1.setBackgroundDrawable(FloatingActionButton.java:118)
at android.support.design.widget.FloatingActionButtonLollipop.setBackgroundDrawable(FloatingActionButtonLollipop.java:75)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:131)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:79)
... 27 more

The only line that points to my app is where it inflates the xml file, and the line in that file which is my FloatingActionButton.

   <android.support.design.widget.FloatingActionButton
        android:id="@+id/myFABSubCat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="16dp"
        android:backgroundTint="@color/accent"
        android:src="@drawable/add_icon"
        app:borderWidth="0dp"
        app:elevation="4sp" />

Extra info:

Here is my app theme:

<resources>

    <!-- Base application theme. -->
    <style name="MBTIAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primaryDark</item>
        <item name="colorAccent">@color/accent</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

    <style name="ThemeNoActionBar" parent="MBTIAppTheme">
        <item name="windowActionBar">false</item>
    </style>

</resources>

Yes, I am using an AppCompatActivity.

I noticed I am using an older version of appcompat:

com.android.support:appcompat-v7:22.1.1

This seems to be up to date:

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

Also,

minSdkVersion 16
targetSdkVersion 22

Now, I do set the Visibility to GONE in some case for my FAB, but this is after it inflates so that can't be the issue.

Now, this works with my 5.1 device, but the users who have issues have 5.0.

Is this a known bug or maybe I am doing something wrong?

Sanjuanitasank answered 16/6, 2015 at 14:24 Comment(3)
android:backgroundTint could be troublesome on devices pre lollipop. Use app:backgroundTint instead. I cant't point out any issues for 5.0 though.Preprandial
@MarkusRubey Thanks. That is worth changing. I changed that and updated my appCompat library in Gradle and checking to see if this fixes the issue. I do know it never crashed on a pre 5.0 device or emulator for me when I was testing even though I had that tint in. But not all devices are created equal...Sanjuanitasank
@MarkusRubey I believe it was your comment that fixed this problem. A problem with tinting which I think is compatible for 5.1 up unless you use app like you say. Feel free to add it as the answer.Sanjuanitasank
P
225

com.android.support:appcompat-v7:21+ added support for tinting widgets on devices running pre android 5.1 (API Level 21). To make use of it make sure you extend or set the AppCompat Theme and use app:backgroundTint instead of android:backgroundTint.

Example:

<android.support.design.widget.FloatingActionButton 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:src="@drawable/icon"
    app:backgroundTint="@color/accent"
    app:borderWidth="0dp" />
Preprandial answered 17/6, 2015 at 15:28 Comment(1)
Note that backgroundTint support was added in API 21 (Android 5.0): developer.android.com/reference/android/… So according to the spec, you should be able to use backgroundTint in Android 5.0 without the support library. In practice, it seems to crash, and you need to use the support library to get it working. I just thought I would mention this for anyone else confused why an API labelled as v21 in the dev documentation doesn't actually work until v22; it seems to be a bug.Crescint
P
50

Just replace

<android.support.design.widget.FloatingActionButton 
...
...
android:backgroundTint
/>

to

<android.support.design.widget.FloatingActionButton 
...
...
app:backgroundTint
/>
Persson answered 3/3, 2016 at 10:23 Comment(2)
Don't forget to add the xmlns:app="schemas.android.com/apk/res-auto" namespace!Megan
problem in my case was setting background with android:background attribute. Replace with app:backgroundTint solved the problemGopak
G
13

If you're using a VectorDrawableCompat (Vector asset) you should use:

app:srcCompat="@drawable/x"

instead of:

android:src="@drawable/x"
Gurl answered 16/10, 2016 at 12:53 Comment(0)
R
7

I had the same issue and tried different solution. But the one that worked for me was to ensure that appcompat and design support library versions are the same. for example:

compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
Renault answered 31/10, 2016 at 16:18 Comment(0)
S
4

Just change android to app:

android:backgroundTint="@color/accent"

To:

app:backgroundTint="@color/accent"
Subminiature answered 20/4, 2018 at 11:32 Comment(1)
Welcome to Stack Overflow! Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. Don't post images of code or error messages. Read why. Instead copy and paste or type the actual code/message into the post directly.Confuse
C
3

Another way to get this message is if you accidentally specified different versions of the appcompat library in different modules. This is likely to happen when you create a new module, since Android Studio defaults to the most current version.

For a tidy way of managing this in multi-module projects, see: In Gradle, how do I declare common dependencies in a single place?

Chadd answered 23/9, 2016 at 20:23 Comment(0)
Z
0

in my case, it was because of the false configuration of activity theme. problem was solved after i changed app theme to Theme.AppCompat.xxx.

Zoon answered 29/3, 2018 at 3:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.