安卓芯片膨胀导致“无效ID 0x00000000”错误。

19

迁移到android-x后,我注意到有很多日志都显示:

"E/itterforandroi: Invalid ID 0x00000000."

我设法缩小到一个新的RecyclerView ViewHolder并填充包含Chip的布局。其他所有字段都没有显示这样的错误,只有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 的自定义实现,请参考 https://github.com/karanatwal/MaterialChipsInput - karanatwal.github.io
我曾经遇到过同样的问题,但是在 Material Components 1.1.0 版本中似乎已经修复了(仍处于 alpha 阶段)。 - Alex Baker
这是由于字体家族资源导致的。即使在芯片中指定了字体家族,TextAppearance.fontFamilyResourceId 被设置为零(0),这会在设备上触发 __Invalid ID 0x00000000__。 metrial-1.0.0 中的 Chip 实现似乎无法处理与字体相关的信息。 - chmin.seo
可以确认在 Material Components 1.0.0 中存在一个 bug,使用新版本(截至目前为止是:1.2.0-alpha02)更新版本即可修复错误。 - rollcage
1
我可以确认,在material:1.3.0-alpha01版本中仍存在此错误,且在应用程序安装后首次启动以及Android杀死应用程序并再次启动时发生。 - Raed
我也看到了相同的日志,但是我无法确定问题出在哪里。你是怎么发现这是芯片的问题的?在我的情况下,这个问题在我自己的手机上没有出现。我在自己的手机上开发了90%的应用程序,然后在一加手机上测试,结果只有在那个手机上才会出现这个问题。 - Karan Malhotra
1个回答

3

我曾经遇到过芯片膨胀的问题,通过使用如下所示的数据绑定布局来解决这一错误。

我认为您应该在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
}

我希望我的解决方案能对您有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接