'How to assign click events in TabItems?

I need to update a list as the user chooses which list to see. I created two items to the TabLayout and I'm trying to get the clik winds but I always get an error.

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/select_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabContentStart="32dp"
        app:tabMode="scrollable">

        <com.google.android.material.tabs.TabItem
            android:id="@+id/posto1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Posto Shell" />

        <com.google.android.material.tabs.TabItem
            android:id="@id/posto2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Posto Guanabara" />
    </com.google.android.material.tabs.TabLayout>

The error: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.tabs.TabItem.setOnClickListener(android.view.View$OnClickListener)' on a null object reference



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source