视图绑定返回 null
在底部工作表对话框中
class XBottomSheet () : BottomSheetDialogFragment() {
private var _binding: XBottomSheetBinding? = null
private val binding get() = _binding!!
private var handlerRunner: Runnable? = null
private val handler = Handler(Looper.getMainLooper())
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = XBottomSheetBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
handlerRunner = Runnable {
binding.tvTimeOffer.text = "text"
}
handler.postDelayed(handlerRunner!!, 1)
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
handlerRunner?.apply {
handler.removeCallbacks(this)
}
}
}
在某些手机中 Firebase carashlytics 日志 private val binding get() = _binding!!
Non-fatal Exception: java.lang.NullPointerException
有时,可以处理程序在设置 _binding = null
后先调用可运行对象,然后再删除它?
我不知道为什么!
In the Bottom sheet dialog
class XBottomSheet () : BottomSheetDialogFragment() {
private var _binding: XBottomSheetBinding? = null
private val binding get() = _binding!!
private var handlerRunner: Runnable? = null
private val handler = Handler(Looper.getMainLooper())
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = XBottomSheetBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
handlerRunner = Runnable {
binding.tvTimeOffer.text = "text"
}
handler.postDelayed(handlerRunner!!, 1)
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
handlerRunner?.apply {
handler.removeCallbacks(this)
}
}
}
In some phones Firebase carashlytics logs private val binding get() = _binding!!
Non-fatal Exception: java.lang.NullPointerException
At some time, can the handler call the runnable before removing it after setting _binding = null
??
I don't know why that!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在绑定获取 null 之前删除处理程序回调
正确的方法
Remove handler callbacks before binding gets null
Correct way
您应该使用 DataBinding
}
从 xml 布局中,通过布局标记封装您的工作:
PS 始终在类名末尾命名您的类类别,以便于搜索和维护
You should use DataBinding instead
}
From xml layout, encapsulate your work by layout tag :
P.S. always name your category of class at the end of your classname for easy search and maintenance