如何检查 "蝴蝶刀 "的装订机是否未装订?

问题描述 投票:0回答:1

有时我收到异常: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。它没有工作与我

android kotlin butterknife postdelayed
1个回答
0
投票

我发现有一个很好的方法可以将unbinder设置为Unbinder.EMPTY。

override fun onDestroyView() {
    super.onDestroyView()
    unbinder.unbind()
    unbinder = Unbinder.EMPTY
}

并在runnable里面检查。

textTv.postDelayed({
    if(unbinder == Unbinder.EMPTY) {
         //do impl here
    }
}, 500)
© www.soinside.com 2019 - 2024. All rights reserved.