【问题标题】:Android material chip component crashing app. Unable to inflate xmlAndroid材料芯片组件崩溃应用。无法膨胀 xml
【发布时间】:2019-01-23 10:57:51
【问题描述】:

已尝试支持库
com.google.android.support:design:28.0.0-rc01 和材料
com.google.android.material:material:1.0.0-rc01 中的芯片 组件

堆栈跟踪

android.view.InflateException: Binary XML file line #72: Binary XML file 
      line #72: Error inflating class com.google.android.material.chip.Chip 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:429)`

布局

<com.google.android.material.chip.Chip
    android:id="@+id/chip"
    style="style/Widget.MaterialComponents.Chip.Entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/account"
    app:closeIconEnabled="true" />`

【问题讨论】:

  • 你试过implementation 'com.android.support:design:28.0.0-rc01'
  • 是的,我做到了。同样的问题
  • 检查我的答案
  • 您是否按照material.io/develop/android/docs/getting-started 中的说明扩展提供的主题?
  • @Paranoid42 感谢您提供的信息。实际上我没有这样做。检查后会回来。

标签: android android-support-library material-components android-chips material-components-android


【解决方案1】:

更新您的应用主题以继承以下主题之一:

Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar

例如:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

注意:使用 Material Components 主题可启用自定义视图充气器

来源: https://www.material.io/develop/android/docs/getting-started/

【讨论】:

  • 我想知道为什么 com.google.android.material 小部件中的任何一个都没有在应用程序中膨胀。这就是原因!
  • 为我节省了许多令人头疼的时间。上帝保佑你
  • 在我的情况下,val chip = new Chip(context)val chip = layoutInflater.from(context).inflate 尽管我的材料主题都失败了。这是因为我传递了错误的context(应用程序上下文而不是活动上下文)。因此,我的材料主题尚未应用。
【解决方案2】:

您可以像这样在 xml 布局中添加 @style/Theme.MaterialComponents.Light 样式属性:

<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="chip"
android:theme="@style/Theme.MaterialComponents.Light"/>

【讨论】:

  • 谢谢!在不更改 App Theme 属性的情况下,它就像一个魅力。
  • 获取此组件的样式要求您的应用主题为 Theme.MaterialComponents(或后代)。
【解决方案3】:

将以下新主题属性添加到您现有的应用主题:

<style name="Theme.MyApp" parent="Theme.AppCompat">

    <!-- Original AppCompat attributes. -->
    <item name="colorPrimary">@color/my_app_primary_color</item>
    <item name="colorPrimaryDark">@color/my_app_primary_dark_color</item>
    <item name="colorAccent">@color/my_app_accent_color</item>

    <!-- New MaterialComponents attributes. -->
    <item name="colorSecondary">?attr/colorPrimary</item>
    <item name="scrimBackground">@color/mtrl_scrim_color</item>
    <item name="textAppearanceHeadline1">@style/TextAppearance.MaterialComponents.Headline1</item>
    <item name="textAppearanceHeadline2">@style/TextAppearance.MaterialComponents.Headline2</item>
    <item name="textAppearanceHeadline3">@style/TextAppearance.MaterialComponents.Headline3</item>
    <item name="textAppearanceHeadline4">@style/TextAppearance.MaterialComponents.Headline4</item>
    <item name="textAppearanceHeadline5">@style/TextAppearance.MaterialComponents.Headline5</item>
    <item name="textAppearanceHeadline6">@style/TextAppearance.MaterialComponents.Headline6</item>
    <item name="textAppearanceSubtitle1">@style/TextAppearance.MaterialComponents.Subtitle1</item>
    <item name="textAppearanceSubtitle2">@style/TextAppearance.MaterialComponents.Subtitle2</item>
    <item name="textAppearanceBody1">@style/TextAppearance.MaterialComponents.Body1</item>
    <item name="textAppearanceBody2">@style/TextAppearance.MaterialComponents.Body2</item>
    <item name="textAppearanceCaption">@style/TextAppearance.MaterialComponents.Caption</item>
    <item name="textAppearanceButton">@style/TextAppearance.MaterialComponents.Button</item>
    <item name="textAppearanceOverline">@style/TextAppearance.MaterialComponents.Overline</item>

</style>

来源:https://material.io/develop/android/docs/getting-started/
谢谢@Paranoid42

【讨论】:

  • 是的,最初这就是问题所在。我使用的是 appcompat 主题
  • 添加这些会在使用芯片时显示黑屏,但至少不会崩溃
【解决方案4】:

终于找到了如何使用新材料芯片动态生成芯片的解决方案

Google 没有提供任何有关chipsInput 布局或如何制作类似布局的gmail 地址的文档。使用 ChipDrawable 不会对芯片选项进行删除操作,因为无法调用 setOnCloseIconClickListener。我可以使用 Horizo​​ntalScrollView、ChipGroup 和 EditText

这是我的代码示例:

MaterialChipsInputDemo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多