Databinding crashes onCreateViewHolder

from the CommonsWare Community archives

At September 12, 2019, 1:44pm, jQrgen asked:

After moving to a single-activity app databinding crashes as follows when creating a viewHolder for a recyclerView.

The problem is that there is little information on what causes the crash (see log below).

I have tried rebuildin and invalidating gradle and android studio caches with no success.

internal class ChatListAdapter (
	private val moduleInstanceId: String
) : ListAdapter<ChatItemData, ChatListAdapter.ViewHolder>(ChatItemDataDiffCallBack){

override fun onCreateViewHolder (parent: ViewGroup, viewType: Int): ViewHolder {
	return ViewHolder( // NOTE: crashes here.
		ItemChatBinding.inflate(
			LayoutInflater.from(parent.context), parent, false
		)
	)
}
...
....
}

xml file the adapter is trying to inflate

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
                name="onChatClickListener"
                type="android.view.View.OnClickListener" />
        <variable
                name="chatItemData"
                type="no.flare.chat.chat.viewModel.ChatItemData"/>
    </data>


    <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="2dp"
            android:onClick="@{onChatClickListener}"
            app:cardCornerRadius="6dp"
            app:contentPadding="16dp"
            app:cardElevation="2dp">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <TextView
                    android:id="@+id/item_chat_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:text="@{chatItemData.chat.id}"
                    android:textColor="@android:color/black"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toStartOf="@+id/chat_item_last_message_time"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:text="Chat Name" />

            <TextView
                    android:id="@+id/chat_item_last_message_author_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{chatItemData.detailText}"
                    android:textColor="@android:color/black"
                    android:textSize="15sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/item_chat_name"
                    tools:text="Last message author name" />

            <TextView
                    android:id="@+id/chat_item_last_message_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{chatItemData.lastMessageTimestampFormatted}"
                    android:textColor="@android:color/black"
                    android:textSize="12sp"
                    app:layout_constraintBottom_toBottomOf="@+id/item_chat_name"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/item_chat_name"
                    tools:text="13:37" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

</layout>
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: no.flare.localchat, PID: 15004
    java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/databinding/DataBinderMapperImpl;
        at androidx.databinding.DataBindingUtil.<clinit>(DataBindingUtil.java:32)
        at androidx.databinding.DataBindingUtil.getDefaultComponent(DataBindingUtil.java:65)
        at no.flare.chat.databinding.ItemChatBinding.inflate(ItemChatBinding.java:60)
        at no.flare.chat.chat.view.ChatListAdapter.onCreateViewHolder(ChatListAdapter.kt:20)
        at no.flare.chat.chat.view.ChatListAdapter.onCreateViewHolder(ChatListAdapter.kt:14)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
        at android.view.View.measure(View.java:23169)
        at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23169)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1676)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
        at android.view.View.measure(View.java:23169)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2718)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1572)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:696)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.databinding.DataBinderMapperImpl" on path: DexPathList[[zip file "/data/app/no.flare.localchat-8I0HCDhRZHuOP-NgkYwsYg==/base.apk"],nativeLibraryDirectories=[/data/app/no.flare.localchat-8I0HCDhRZHuOP-NgkYwsYg==/lib/x86, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        	... 61 more

At September 12, 2019, 4:09pm, jQrgen replied:

This stackoverflow post was the solution: https://stackoverflow.com/a/44497106/2333802


At September 12, 2019, 10:30pm, mmurphy replied:

Glad that you got it working!