java.lang.IllegalStateException: Bindings already cleared.

After upgrading from 7.x.x to 10.x.x using a ButterKnife, the Fragment method in a viewPager reported the following error:

java.lang.IllegalStateException: Bindings already cleared.

Copy the code

View the code git history from

 ButterKnife.unbind(this);
Copy the code

Into the

  unbinder.unbind();
Copy the code

And then I realized,

The parent class:

@Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
Copy the code

A subclass

  @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
Copy the code

Unbinder.unbind (); It is assumed that the parent class has already performed unbind(), and the subclass will perform the exception raised by unbind().

So jump into unbind() and check the source code:

public interface Unbinder {
  @UiThread void unbind();

  Unbinder EMPTY = () -> { };
}
Copy the code

Select unbind and then use the shortcut option+ Command +B to skip to the implementation:

@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;
  }
Copy the code

New IllegalStateException(“Bindings already cleared.”);

Unbinder.unbind () :

  @Override
    public void onDestroyView() {
        super.onDestroyView();
        // unbinder.unbind();
    }
Copy the code