16

After moving to android-x I noticed that there are plenty of logs saying:

"E/itterforandroi: Invalid ID 0x00000000."

I manage to circle down to the point where I'm creating a new RecyclerView ViewHolder and inflating a layout that contains a Chip. Every other field does not show such an error, only Chip.

In the xml it's looking like so:

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

I cannot find what is really missing in this definition that causes the error. Any hint?

4

1 回答 1

2

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

我还认为您应该删除 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
}

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

于 2020-08-22T01:14:15.863 回答