gpt4 book ai didi

android - 获取 java.lang.RuntimeException : Failed to call observer method from binding implementation method

转载 作者:行者123 更新时间:2023-11-29 02:20:48 28 4
gpt4 key购买 nike

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

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

而且我检查过这个方法返回的不是空值。

在 ViewModel 中,我这样做:

private val ethRepository: EthRepository = EthRepository(application)

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

在 fragment 中,我尝试获取 viewModel 并绑定(bind)绑定(bind):

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

我的回收 View 的适配器:

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
}

}
}
}

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

但是当我在这个 fragment 中启动我的应用程序时,我在数据绑定(bind)生成的 FragmentJournalBindingImpl 中得到了异常(exception):

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)

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

最佳答案

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

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

所以改变

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

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

关于android - 获取 java.lang.RuntimeException : Failed to call observer method from binding implementation method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55887797/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com