【问题标题】:Android Chip inflation causes "Invalid ID 0x00000000."Android 芯片膨胀导致“无效 ID 0x00000000”。
【发布时间】:2019-04-03 21:26:36
【问题描述】:

在迁移到 android-x 之后,我注意到有很多日志说:

"E/itterforandroi: Invalid ID 0x00000000."

我设法向下循环到我正在创建一个新的 RecyclerView ViewHolder 并膨胀包含芯片的布局。其他所有字段都没有显示这样的错误,只有 Chip。

在 xml 中它看起来像这样:

<com.google.android.material.chip.Chip
    android:id="@+id/someChip"
    style="@style/Widget.MaterialComponents.Chip.Action"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|center"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    app:chipIcon="@drawable/ic_account_circle_black_24dp"
    app:chipIconEnabled="true" />

我找不到导致错误的定义中真正缺少的内容。有什么提示吗?

【问题讨论】:

  • 对于AndroidX自定义实现github.com/karanatwal/MaterialChipsInput
  • 我遇到了同样的问题,但它似乎已在材料组件 1.1.0 中修复(仍处于 alpha 阶段)
  • 这是因为字体系列资源。即使在芯片中指定了字体系列,TextAppearance.fontFamilyResourceId 也会设置为零 (0),这会在每个设备上触发 Invalid ID 0x00000000。 metrial-1.0.0 中的芯片实现似乎无法处理字体相关信息。
  • 可以确认这是material components 1.0.0的bug,使用新版本时(目前为:1.2.0-alpha02)更新版本,错误会修复
  • 我可以确认这个 bug 仍然存在于 material:1.3.0-alpha01 中,并且在安装后首次启动应用程序以及 android 杀死应用程序并重新启动它时发生。

标签: android android-chips


【解决方案1】:

我在芯片膨胀方面遇到了同样的问题,我通过切换到我的芯片的数据绑定布局来修复这个错误,如下所示。

我还认为您应该删除 xml 中的“android:id”属性,因为您正在使用 RecyclerView 动态填充芯片。它应该为你自动生成一个 id。

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <data>
        <import type="android.content.Context" />
        <variable
            name="element"
            type="com.android.sarahmica.app.database.Element" />
    </data>
    
    <com.google.android.material.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:clickable="true"
        android:focusable="true"
        android:text="@{element.name}"
        android:tag="@{element.id}"
        app:chipBackgroundColor="@{element.getChipColor(context)}"
        style="@style/Widget.MaterialComponents.Chip.Filter" />
</layout>

然后我像这样从片段中的列表中填充我的芯片(但由于您使用的是 RecycylerView,因此您必须调整您的适配器以相应地使用数据绑定)

val chipGroup = getChipGroup(type)
val inflater = LayoutInflater.from(chipGroup.context)
elementList.forEach { element ->
    val chipBinding: MyChipBinding = DataBindingUtil.inflate(inflater, R.layout.my_chip, chipGroup, true)
    chipBinding.greenActivity = activity
    binding.lifecycleOwner = this
}

希望我的解决方案能帮到你!!

【讨论】:

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