Butterknife: Unable to bind views for ViewHolder

Created on 24 Dec 2015  Â·  6Comments  Â·  Source: JakeWharton/butterknife

public class CustomViewHolder extends RecyclerView.ViewHolder {
  @Nullable @Bind(R.id.view1) public View mView1;
  @Nullable @Bind(R.id.view2) public View mView2;
   ....
   public CustomViewHolder(View view) {
    super(view);

    assert view != null;

    ButterKnife.bind(this, view); // line 22
  }
}

Not sure why I keep hitting the following error.

java.lang.RuntimeException: Unable to bind views for  ....holders.CustomViewHolder
                   at butterknife.ButterKnife.bind(ButterKnife.java:322)
                   at butterknife.ButterKnife.bind(ButterKnife.java:279)
                   at ...holders.CustomViewHolder.<init>(CustomViewHolder.java:22)

Most helpful comment

Oops! Seems like I goofed up. Anyways, my takeaway.

Since the original Exception which caused the error is passed into a new RuntimeException (here), a better debug output can be seen when you wrap the binding with a try/catch. This will expose the "Caused by" exception (Android Studio 1.5)

try {
    ButterKnife.bind(this, view);
} catch (Exception e) {
    e.printStackTrace();
}

All 6 comments

Paste the full trace

On Wed, Dec 23, 2015, 10:03 PM Mathew Kurian [email protected]
wrote:

public class CustomViewHolder extends ViewHolder {
@Nullable @Bind(R.id.view1) public View mView1;
@Nullable @Bind(R.id.view2) public View mView2;
....
public CustomViewHolder(View view) {
super(view);

assert view != null;

ButterKnife.bind(this, view); // line 22

}
}

_Not sure why I keep hitting the following error._

java.lang.RuntimeException: Unable to bind views for ....holders.CustomViewHolder
at butterknife.ButterKnife.bind(ButterKnife.java:322)
at butterknife.ButterKnife.bind(ButterKnife.java:279)
at ...holders.CustomViewHolder.(CustomViewHolder.java:22)

—
Reply to this email directly or view it on GitHub
https://github.com/JakeWharton/butterknife/issues/423.

Oops! Seems like I goofed up. Anyways, my takeaway.

Since the original Exception which caused the error is passed into a new RuntimeException (here), a better debug output can be seen when you wrap the binding with a try/catch. This will expose the "Caused by" exception (Android Studio 1.5)

try {
    ButterKnife.bind(this, view);
} catch (Exception e) {
    e.printStackTrace();
}

Yeah this is a problem for long traces inside of Android's code to handle printing these out. Glad you got it resolved, anyway.

Hi Jake, I wonder how we can unbind view for recycler's view holder ? I cannot find any destructor method for view holder so It can lead to memory leak issue in future!

@harry-nguyen-88 you can add a RecyclerListener to the RecyclerView, keep a reference to the binding in the ViewHolder, and then call viewHolder.unbinder.unbind() in onViewRecycled.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shehabic-work picture shehabic-work  Â·  3Comments

Pitel picture Pitel  Â·  3Comments

hwra2008 picture hwra2008  Â·  3Comments

engr-umar-qureshi picture engr-umar-qureshi  Â·  3Comments

sdobek picture sdobek  Â·  4Comments