我在我的应用程序中使用实时数据进行所有网络调用和响应处理。
在其中一种情况下,我的回收器视图正在其视图持有者的视图中加载一些数据,onBind并且响应正在更新 UI。为此,我必须向lifecycleOwner观察者提供一个。
由于回收器视图没有自己的任何生命周期所有者,因此我正在使用父片段,parentFragment.viewlifecycleOwner但不知何故它给出了错误。
当父片段没有实例时,视图持有者如何拥有其实例?
viewModel.responseState.observe(parentFragment.viewLifecycleOwner, Observer {
updateUI(it)
})
Run Code Online (Sandbox Code Playgroud)
致命异常:java.lang.IllegalStateException:当 getView() 为 null 时,即在 onCreateView() 之前或 onDestroyView() 之后,无法访问 Fragment View 的 LifecycleOwner