android - How to check if binder of Butterknife is unbinded
问题描述
Sometimes I got exception: kotlin.UninitializedPropertyAccessExceptionlateinit property textTv has not been initialized
declaration of view:
@BindView(R.id.tv) internal lateinit var textTv: RipplePulseLayout
rest are classic ButterKnife init:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// bind view using butter knife
unbinder = ButterKnife.bind(this, rootView);
textTv.postDelayed({
//do impl here
}, 500)
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
After tracking the issue I found that when using widget in separated thread like postDelayed
Traditional approach to make textTv nullable .. I wonder if there is better solution. I tried removeCallback of textTv in onDestroyView. It did not work with me
解决方案
I found there is a nice way to set unbinder to Unbinder.EMPTY
override fun onDestroyView() {
super.onDestroyView()
unbinder.unbind()
unbinder = Unbinder.EMPTY
}
and check that inside runnable:
textTv.postDelayed({
if(unbinder == Unbinder.EMPTY) {
//do impl here
}
}, 500)
推荐阅读
- google-apps-script - 根据模板从选定的行生成新的电子表格
- geometry - 如何用 CSS 重现这个形状?
- cypress - 赛普拉斯:从变量中提取文本和数字
- amazon-web-services - 访问 AWS Route 53 域时出现问题
- java - 如何在 actionListener(actionEvent e) 中声明 JLabels 以便在单击 JButton 时设置 Text
- c# - 控制台将希腊字符显示为“?” C#
- aws-amplify-sdk-android - 为具有复杂对象的 android 实现放大数据存储
- javascript - react-spring 如何使用动画(组件)
- php - 获取 Google Analytics API 令牌/Google_Client getAccessToken 返回 null/空
- django - Django:如何生成唯一的订单 ID