【发布时间】:2021-11-28 13:30:53
【问题描述】:
我正在实施ViewBinding,根据 Google 文档,我们应该这样做:
private var _binding: ResultProfileBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
....
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
我们强制 binding 为非空,但我们最终可能会遇到这样的情况:
binding.itemView.post {
// when post is called, fragment is still available but when the runnable run's fragment isn't available.
binding.itemView.setText="......"
}
我们可以让 getter 返回 null,但在这种情况下,我们需要到处使用 binding?.。
如何处理此问题以使binding.itemView.setText="......" 不会导致应用崩溃?
【问题讨论】:
标签: android android-viewbinding