使用 ButterKnife 從 7.x.x 升級到 10.x.x 后油昂,某個(gè) viewPager 中的
的 Fragment 方法報(bào)以下錯(cuò)誤:
java.lang.IllegalStateException: Bindings already cleared.
查看代碼git歷史乳蓄,是從
ButterKnife.unbind(this);
變?yōu)榱?/p>
unbinder.unbind();
然后發(fā)現(xiàn)镜豹,
父類:
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
子類
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
中都調(diào)用了 unbinder.unbind();
猜測是因?yàn)楦割愐呀?jīng)執(zhí)行過unbind()
了夜惭,子類再去執(zhí)行unbind()引發(fā)的異常。
所以跳進(jìn)unbind()
查看源碼:
public interface Unbinder {
@UiThread void unbind();
Unbinder EMPTY = () -> { };
}
選中unbind
,然后用快捷鍵 option+command+B,跳到實(shí)現(xiàn)中:
@Override
@CallSuper
public void unbind() {
AbsWebViewActivity target = this.target;
if (target == null) throw new IllegalStateException("Bindings already cleared.");
this.target = null;
target.mWebView = null;
target.tvLoadFail = null;
target.btnLoadAgain = null;
target.llLoadError = null;
target.llProgressbar = null;
}
果然發(fā)現(xiàn)了new IllegalStateException("Bindings already cleared.");
所以子類刪除 unbinder.unbind()
就可以了跷叉,子類修改如下:
@Override
public void onDestroyView() {
super.onDestroyView();
// unbinder.unbind();
}