应用 ButterKnife 从 7.x.x 降级到 10.x.x 后,某个 viewPager 中的
的 Fragment 办法报以下谬误:
java.lang.IllegalStateException: Bindings already cleared.
查看代码 git 历史,是从
ButterKnife.unbind(this);
变为了
unbinder.unbind();
而后 发现,
父类:
@Override
public void onDestroyView() {super.onDestroyView();
unbinder.unbind();}
子类
@Override
public void onDestroyView() {super.onDestroyView();
unbinder.unbind();}
中都调用了 unbinder.unbind();
猜想是因为父类曾经执行过 unbind()
了,子类再去执行 unbind()引发的异样。
所以跳进 unbind()
查看源码:
public interface Unbinder {@UiThread void unbind();
Unbinder EMPTY = () -> {};
}
选中unbind
, 而后用快捷键 option+command+B, 跳到实现中:
@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;
}
果然发现了new IllegalStateException("Bindings already cleared.");
所以子类删除 unbinder.unbind()
就能够了,子类批改如下:
@Override
public void onDestroyView() {super.onDestroyView();
// unbinder.unbind();}