Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get java.lang.RuntimeException: Failed to call observer method from binding implementation method

please help me with my problem. I'd like to get LiveData from my DataBase and and display it with recycleview. I have method in Repository to get it from database:

fun getEthTransactions(): LiveData<List<EthTransaction>> {
        return mEthTransactionsDao.getTransactions()
    }

And I checked that this method returns not null.

In ViewModel I do this:

private val ethRepository: EthRepository = EthRepository(application)

    val transactionData: LiveData<List<EthTransaction>>
        get() = ethRepository.getEthTransactions()

In fragment I try to get viewModel and tie with binding:

private val journalFragmentViewModel by lazy {
        ViewModelProviders.of(this).get(JournalFragmentViewModel::class.java)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val activityJournalBinding : FragmentJournalBinding =  DataBindingUtil.inflate(inflater, R.layout.fragment_journal, container,false)

        journalFragmentViewModel.transactionData.observe(this, Observer {
            activityJournalBinding.viewModel = journalFragmentViewModel
            JournalAdapter().journal = it
        })

        activityJournalBinding.viewModel = journalFragmentViewModel
        activityJournalBinding.lifecycleOwner = this
        return activityJournalBinding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val layoutManager = LinearLayoutManager(context)

        recyclerView.layoutManager = layoutManager
        recyclerView.hasFixedSize()
        recyclerView.adapter = JournalAdapter()
        recyclerView.addItemDecoration(DividerItemDecoration(context, layoutManager.orientation))
    }

Adapter of my recycleview:

class JournalAdapter : RecyclerView.Adapter<JournalAdapter.ViewHolder>() {

    var journal: List<EthTransaction> = emptyList()
        set(value){
            field = value
            notifyDataSetChanged()
        }

    override fun getItemCount() = journal.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(journal[position])
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        return ViewHolder(
           DataBindingUtil.inflate(
               LayoutInflater.from(parent.context),
               R.layout.view_journal,
               parent,
               false))
    }

    companion object {
        @JvmStatic
        @BindingAdapter("journal")
        fun RecyclerView.bindItems(journal: List<EthTransaction>) {
            val adapter = adapter as JournalAdapter
            adapter.journal = journal
        }
    }

    class ViewHolder (private val binding: ViewJournalBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bind (item: EthTransaction) {

            with(binding) {
                viewJournalAccount.text = item.account
                viewJournalAmount.text = item.amount
            }

        }
    }
}

In resources of fragment I'm trying to add info from viewModel:

<LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">

       <androidx.recyclerview.widget.RecyclerView
               android:id="@+id/recyclerView"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               app:journal="@{viewModel.transactionData}"
                />
   </LinearLayout>

But when I start my app in this fragment I get exeption in FragmentJournalBindingImpl generated by databinding:

java.lang.RuntimeException: Failed to call observer method
        at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:226)
        at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:194)
        at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:185)
        at androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:36)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:300)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:2590)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:918)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1235)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1301)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:710)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2071)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1861)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1816)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1723)
        at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManagerImpl.java:146)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5444)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:727)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617)
     Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter journal
        at hellynx.poscryptos.ui.fragments.journal_fragment.JournalAdapter$Companion.bindItems(JournalAdapter.kt)
        at hellynx.poscryptos.ui.fragments.journal_fragment.JournalAdapter.bindItems(JournalAdapter.kt)
        at hellynx.poscryptos.databinding.FragmentJournalBindingImpl.executeBindings(FragmentJournalBindingImpl.java:129)
        at androidx.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:463)
        at androidx.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:435)
        at androidx.databinding.ViewDataBinding$OnStartListener.onStart(ViewDataBinding.java:1674)
        at java.lang.reflect.Method.invoke(Native Method)

I suppose that I am incorrectly addressing the livedata, but of all the examples I have studied, I still do not understand how to do this correctly, please help.

like image 486
Ivan Black Avatar asked Apr 28 '19 06:04

Ivan Black


1 Answers

In your code, problem lies in companion object of your JournalAdapter

Adapter accepts nullable list.

So change

fun RecyclerView.bindItems(journal: List<EthTransaction>)

to

fun RecyclerView.bindItems(journal: List<EthTransaction>?)

like image 185
Ananth Avatar answered Nov 15 '22 19:11

Ananth