有时我收到异常:kotlin.UninitializedPropertyAccessExceptionlateinit属性textTv未被初始化。
观点的声明。
@BindView(R.id.tv) internal lateinit var textTv: RipplePulseLayout
其余的都是经典的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();
}
在追踪问题后,我发现当在分离的线程中使用widget时,比如postDelayed。
传统的方法是让textTv可置空......我想知道是否有更好的解决方案。我试过在onDestroyView中删除TextTv的Callback。它没有工作与我
我发现有一个很好的方法可以将unbinder设置为Unbinder.EMPTY。
override fun onDestroyView() {
super.onDestroyView()
unbinder.unbind()
unbinder = Unbinder.EMPTY
}
并在runnable里面检查。
textTv.postDelayed({
if(unbinder == Unbinder.EMPTY) {
//do impl here
}
}, 500)