RecyclerView.ViewHolder unable to bind view with ButterKnife
Asked Answered
D

2

29

I'm using ButterKnife to bind my views on my ViewHolder. My code is below:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<DataObject> data;

    public MyAdapter(List<DataObject> data) {
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_layout, parent, false);
        return new ViewHolder(view);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.row_header_view) View rowHeaderView;
        @BindView(R.id.row_header_view_text) TextView headerTextView;

        @BindView(R.id.row_data_view) View rowDataView;
        @BindView(R.id.row_data_view_text) TextView rowDataTextView;
        @BindView(R.id.row_data_view_detail_text) TextView rowDataDetailTextView;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}

For some reason in my ViewHolder all of the BindView's do nothing. They are all null. I can confirm with certainty they are in my layout. What is wrong with my above code? I have used it as per the documentation listed here:

http://jakewharton.github.io/butterknife/#reset

Is there anything else required? I'm using ButterKnife version:

compile 'com.jakewharton:butterknife:8.2.1'

If I add the below line:

rowHeaderView = view.findViewById(R.id.row_header_view);

It's able to get the view properly. But how does this make sense? Isn't ButterKnife usable where findViewById is usable?

Diploma answered 17/7, 2016 at 6:16 Comment(3)
Do you have the apt 'com.jakewharton:butterknife-compiler:8.2.1' dependency?Bloomfield
Yes, it's in my gradle file.Diploma
@Diploma can you apply plugin like this apply plugin: 'android-apt' in your module.build ?Postprandial
P
11

Make sure that if you have use ButterKnife library to use this way

build.gradle file of Project

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

Then, apply the 'android-apt' plugin in your module-level build.gradle and add the Butter Knife dependencies:

apply plugin: 'android-apt'

android {
  ...
}

dependencies {
  compile 'com.jakewharton:butterknife:8.2.1'
  apt 'com.jakewharton:butterknife-compiler:8.2.1'
}
Postprandial answered 17/7, 2016 at 7:43 Comment(4)
@Nullable refers to fields that are optional. Mine are all there. This answer is incorrect.Diploma
@Diploma can you apply plugin like this apply plugin: 'android-apt' in your module.build ?Postprandial
Ahhh yes. that was missing. I fixed it. If you update your answer I will accept it.Diploma
@Diploma glad to help you.Postprandial
S
23

Update for October 2016: You probably don't need apt and the android-apt plugin anymore. Version 2.2 of the Android Gradle plugin has an annotationProcessor configuration that you should be using instead.

In Android studio 2.2 onward, just add these on your build.gradle module :

compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
Swellhead answered 2/1, 2017 at 11:27 Comment(0)
P
11

Make sure that if you have use ButterKnife library to use this way

build.gradle file of Project

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

Then, apply the 'android-apt' plugin in your module-level build.gradle and add the Butter Knife dependencies:

apply plugin: 'android-apt'

android {
  ...
}

dependencies {
  compile 'com.jakewharton:butterknife:8.2.1'
  apt 'com.jakewharton:butterknife-compiler:8.2.1'
}
Postprandial answered 17/7, 2016 at 7:43 Comment(4)
@Nullable refers to fields that are optional. Mine are all there. This answer is incorrect.Diploma
@Diploma can you apply plugin like this apply plugin: 'android-apt' in your module.build ?Postprandial
Ahhh yes. that was missing. I fixed it. If you update your answer I will accept it.Diploma
@Diploma glad to help you.Postprandial

© 2022 - 2024 — McMap. All rights reserved.