设计支持库 22.2.1 中的 Snackbar InflateException

Posted

技术标签:

【中文标题】设计支持库 22.2.1 中的 Snackbar InflateException【英文标题】:Snackbar InflateException in Design Support Library 22.2.1 【发布时间】:2015-10-19 16:16:48 【问题描述】:

我正在尝试使用 Design Support Library 22.2.1 中的 Snackbar,我收到了 InflateException

这就是我的使用方式:

Snackbar.make(searchQueryButton, "Hello world", Snackbar.LENGTH_LONG).show();

这是个例外:

    java.lang.RuntimeException: Unable to start activity ComponentInfocom.myapp.android.search.test_app/com.myapp.android.search.test_app.MainActivity: android.view.InflateException: Binary XML file line #18: Error inflating class android.support.design.widget.Snackbar$SnackbarLayout
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class android.support.design.widget.Snackbar$SnackbarLayout
            at android.view.LayoutInflater.createView(LayoutInflater.java:633)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.support.design.widget.Snackbar.<init>(Snackbar.java:127)
            at android.support.design.widget.Snackbar.make(Snackbar.java:148)
            at com.myapp.android.search.test_app.MainActivity.onCreate(MainActivity.java:60)
            at android.app.Activity.performCreate(Activity.java:6010)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     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)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.support.design.widget.Snackbar.<init>(Snackbar.java:127)
            at android.support.design.widget.Snackbar.make(Snackbar.java:148)
            at com.myapp.android.search.test_app.MainActivity.onCreate(MainActivity.java:60)
            at android.app.Activity.performCreate(Activity.java:6010)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class TextView
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.design.widget.Snackbar$SnackbarLayout.<init>(Snackbar.java:534)
            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)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.support.design.widget.Snackbar.<init>(Snackbar.java:127)
            at android.support.design.widget.Snackbar.make(Snackbar.java:148)
            at com.myapp.android.search.test_app.MainActivity.onCreate(MainActivity.java:60)
            at android.app.Activity.performCreate(Activity.java:6010)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 13
            at android.content.res.TypedArray.getDrawable(TypedArray.java:747)
            at android.view.View.<init>(View.java:3742)
            at android.widget.TextView.<init>(TextView.java:637)
            at android.widget.TextView.<init>(TextView.java:632)
            at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:44)
            at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:40)
            at android.support.v7.internal.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
            at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:806)
            at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:836)
            at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.design.widget.Snackbar$SnackbarLayout.<init>(Snackbar.java:534)
            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)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.support.design.widget.Snackbar.<init>(Snackbar.java:127)
            at android.support.design.widget.Snackbar.make(Snackbar.java:148)
            at com.myapp.android.search.test_app.MainActivity.onCreate(MainActivity.java:60)
            at android.app.Activity.performCreate(Activity.java:6010)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

关于如何解决这个问题的任何想法?这是图书馆的问题吗?

顺便说一句,我在 Lollipop 5.1 上运行它,并且我的所有活动都从 AppCompatActivity 扩展并具有正确的主题。

编辑:

我意识到主题就是问题所在。如果我让我的活动使用Theme.AppCompat.Light.NoActionBar,它会按预期工作。

但是,如果我使用自己的主题(覆盖 Theme.AppCompat.Light.NoActionBar),它会抛出我上面描述的异常。

这是我的主题:

    <style name="Theme.MLTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">

        <item name="android:windowBackground">@color/background_color</item>
        <item name="android:windowActionModeOverlay">true</item>
        <item name="vpiCirclePageIndicatorStyle">@style/CustomCirclePageIndicator</item>

        <item name="android:actionMenuTextColor">@color/gray_dark</item>

        <item name="actionBarTabBarStyle">@style/Widget.MLTheme.ActionBar</item>
        <item name="actionBarStyle">@style/Widget.MLTheme.ActionBar</item>
        <item name="actionBarTabStyle">@style/Widget.MLTheme.ActionBar.TabView</item>
        <item name="actionBarTabTextStyle">@style/Widget.MLTheme.ActionBar.TabText</item>

        <item name="android:buttonStyle">@style/MLButton.Primary</item>
        <item name="buttonBarButtonStyle">@style/MLButton.Primary</item>

        <item name="android:textViewStyle">@style/textview_default</item>
        <item name="android:progressBarStyle">@style/Widget.MyApp.ProgressBar.Horizontal</item>
        <item name="android:progressBarStyleHorizontal">@style/Widget.MyApp.ProgressBar.Horizontal</item>
        <item name="android:progressBarStyleLarge">@style/Widget.MyApp.ProgressBar.Horizontal</item>
        <item name="android:scrollViewStyle">@style/Widget.ScrollView.Default</item>

        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

        <item name="colorAccent">@color/blue</item>

        <item name="android:actionBarSize">@dimen/abc_action_bar_default_height_material</item>

        <item name="android:colorPressedHighlight">@color/selection_color</item>
        <item name="android:colorLongPressedHighlight">@color/selection_color</item>

        <item name="android:spinnerItemStyle">@style/MLSpinnerItem</item>

        <item name="android:listSeparatorTextViewStyle">@style/MLPreference.ListHeader</item>

        <item name="android:dropDownItemStyle">@style/MLDropDownItemStyle</item>

        <item name="actionBarItemBackground">?attr/selectableItemBackgroundBorderless</item>
        <!-- for native ActionBar -->
        <item name="android:actionBarItemBackground">?attr/selectableItemBackgroundBorderless</item>

        <item name="selectableItemBackground">?attr/selectableItemBackground</item>

    </style>

编辑 2:

我发现如果我删除主题中的最后一个条目,一切都会再次按预期运行。

&lt;item name="selectableItemBackground"&gt;?attr/selectableItemBackground&lt;/item&gt;

有谁知道为什么会导致异常?另外,有谁知道如何通过保持 Snackbar 工作来替换该条目以达到相同的效果?

谢谢。

【问题讨论】:

查看您的 XML 会很有用,因为`XML 文件第 18 行:错误` 您是否在您的小吃店上使用该属性?如果您发布视图xml会更容易。同时,您可能想检查一些类似的问题,例如***.com/questions/23969769/… 【参考方案1】:

好的,我终于通过改变这个解决了这个问题:

&lt;item name="selectableItemBackground"&gt;?attr/selectableItemBackground&lt;/item&gt;

...到这个:

&lt;item name="selectableItemBackground"&gt;?android:attr/selectableItemBackground&lt;/item&gt;

我怀疑如果您将 AppCompat 升级到 22.2.1,您还必须更改该属性。

【讨论】:

以上是关于设计支持库 22.2.1 中的 Snackbar InflateException的主要内容,如果未能解决你的问题,请参考以下文章

Snackbar 问题(仅在调试模式下)

Android 多行 Snackbar

Material Design ,Snackbar的使用

无法访问类“android.support.design.widget.Snackbar”

圆形 Android Wear 设计的 Snackbar

获取小吃店支持库的高度