Android Chip inflation causes "Invalid ID 0x00000000."

Fiszu picture Fiszu · Apr 3, 2019 · Viewed 11.7k times · Source

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?

Answer

Sarah Mica picture Sarah Mica · Aug 22, 2020

I was having the same issue with chip inflation and I fixed this error by switching to a data binding layout for my chip as shown below.

I also think you should remove your "android:id" property in the xml since you are using a RecyclerView to populate chips dynamically. It should autogenerate an id for you.

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

And then I inflate my chips from an a list in the fragment like so (but since you are using a RecycylerView, you'll have to adjust your adapter to use databinding accordingly)

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
}

I hope my solution helps you!!