获取java.lang.RuntimeException:无法从绑定实现方法调用观察者方法

时间:2019-04-28 06:57:32

标签: android android-room android-databinding android-livedata android-mvvm

请帮助我解决我的问题。 我想从我的数据库中获取LiveData并使用recycleview显示它。 我在存储库中有一种方法可以从数据库中获取它:

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

我检查了此方法是否返回非null。

在ViewModel中,我这样做:

private val ethRepository: EthRepository = EthRepository(application)

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

在片段中,我尝试获取viewModel并绑定:

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

我的回收站适配器:

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
            }

        }
    }
}

在片段资源中,我试图从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>

但是当我在这个片段中启动我的应用程序时,我得到了由数据绑定生成的FragmentJournalBindingImpl中的证据:

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)

我想我没有正确处理实时数据,但是在我研究的所有示例中,我仍然不知道如何正确执行此操作,请帮忙。

1 个答案:

答案 0 :(得分:0)

在您的代码中,问题出在您的companion object中的JournalAdapter

适配器接受可为空的列表。

所以改变

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

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