使用官方设计库中的FloatingActionButton进行InflateException

时间:2015-06-16 14:24:27

标签: android android-appcompat inflate-exception floating-action-button android-support-design

我使用Google支持设计库中的官方FloatingActionButton收到错误。

这是我的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

指向我的应用的唯一一行是xml文件膨胀的位置,以及该文件中我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" />

额外信息:

这是我的应用主题:

<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>

是的,我使用的是AppCompatActivity

我注意到我使用的是旧版appcompat:

com.android.support:appcompat-v7:22.1.1

这似乎是最新的:

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

此外,

minSdkVersion 16
targetSdkVersion 22

现在,我确实在某些情况下为我的FAB将可见性设置为GONE,但这是在它膨胀之后,这不是问题。

现在,这适用于我的5.1设备,但有问题的用户有5.0。

这是一个已知的错误还是我做错了什么?

7 个答案:

答案 0 :(得分:223)

com.android.support:appcompat-v7:21+在运行pre android 5.1(API Level 21)的设备上添加了对小部件着色的支持。要使用它,请务必扩展或设置AppCompat主题并使用app:backgroundTint代替android:backgroundTint

示例:

<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" />

答案 1 :(得分:47)

只需替换

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

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

答案 2 :(得分:10)

如果您使用VectorDrawableCompat(矢量资产),则应使用:

app:srcCompat="@drawable/x"

而不是:

android:src="@drawable/x"

答案 3 :(得分:7)

我有同样的问题并尝试了不同的解决方案。但对我有用的是确保appcompat和设计支持库版本相同。 例如:

compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'

答案 4 :(得分:3)

获取此消息的另一种方法是,如果您在不同模块中意外指定了不同版本的appcompat库。创建新模块时可能会发生这种情况,因为Android Studio默认为最新版本。

有关在多模块项目中进行管理的整洁方法,请参阅:In Gradle, how do I declare common dependencies in a single place?

答案 5 :(得分:3)

只需将android更改为app

android:backgroundTint="@color/accent"

要:

app:backgroundTint="@color/accent"

答案 6 :(得分:0)

在我的情况下,这是因为活动主题的错误配置。我将app主题更改为Theme.AppCompat.xxx后问题解决了。