ChatKit icon indicating copy to clipboard operation
ChatKit copied to clipboard

Custom ViewHolder not working with Kotlin

Open paulelpidorou opened this issue 4 years ago • 5 comments

I am currently developing an app with Kotlin and when i declare custom ViewHolder written in kotlin, i get the following exception when trying to inflate them

2020-04-18 22:05:14.510 24631-24631/com.adelve.findmyteacher E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.adelve.findmyteacher, PID: 24631
    java.lang.UnsupportedOperationException: Somehow we couldn't create the ViewHolder for message. Please, report this issue on GitHub with full stacktrace in description.
        at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolders.java:640)
        at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolders.java:615)
        at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolders.java:558)
        at com.stfalcon.chatkit.messages.MessagesListAdapter.onCreateViewHolder(MessagesListAdapter.java:99)
        at com.stfalcon.chatkit.messages.MessagesListAdapter.onCreateViewHolder(MessagesListAdapter.java:47)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7131)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6260)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6143)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6139)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2314)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:643)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4141)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3853)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4410)
        at android.view.View.layout(View.java:20672)
        at android.view.ViewGroup.layout(ViewGroup.java:6194)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
        at android.view.View.layout(View.java:20672)
        at android.view.ViewGroup.layout(ViewGroup.java:6194)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20672)
        at android.view.ViewGroup.layout(ViewGroup.java:6194)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1656)
        at android.view.View.layout(View.java:20672)
        at android.view.ViewGroup.layout(ViewGroup.java:6194)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20672)
        at android.view.ViewGroup.layout(ViewGroup.java:6194)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:20672)
        at android.view.ViewGroup.layout(ViewGroup.java:6194)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20672)
        at android.view.ViewGroup.layout(ViewGroup.java:6194)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:20672)
        at android.view.ViewGroup.layout(ViewGroup.java:6194)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
        at android.view.View.layout(View.java:20672)
        at android.view.ViewGroup.layout(ViewGroup.java:6194)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
2020-04-18 22:05:14.511 24631-24631/com.adelve.findmyteacher E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        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.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolders.java:629)
        	... 65 more
     Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter payload
        at com.adelve.findmyteacher.ui.components.view_holders.messaging.OutcomingAudioMessageViewHolder.<init>(Unknown Source:7)
        	... 68 more

I am assuming it has to do with how you get the adapter under the hood or something? When i use the same viewholder but written in Java everything works ok

paulelpidorou avatar Apr 18 '20 19:04 paulelpidorou

I had same issue, in custom ViewHolder's constuctor you should declare payload parameter as optional object type (Any?).

vinczea avatar May 19 '20 09:05 vinczea

Screen Shot 2020-12-04 at 18 20 54 i think this's same with ur issue, can anyone help? Thanks so much

fukemy avatar Dec 04 '20 11:12 fukemy

@fukemy Here is an example: class CustomViewHolder(itemView: View, payload: Any?) : MessageHolders.BaseOutcomingMessageViewHolder<Message>(itemView, payload)

vinczea avatar Dec 04 '20 11:12 vinczea

thanks u so much

fukemy avatar Dec 04 '20 11:12 fukemy

I ended up creating my ViewHolders on Java instead of kotlin in order to have a constructor. I'm very disappointed with the solution tbh.

foliolo avatar Aug 03 '22 20:08 foliolo