I'm trying to use Butter Knife in a custom adapter like the documentation advertises: http://jakewharton.github.io/butterknife/
I pasted the example code and inserted the layout with my RelativeLayout
file:
@Override public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
ButterKnife.setDebug(true);
if (view != null) {
holder = (ViewHolder) view.getTag();
} else {
view = inflater.inflate(R.layout.list_row_people, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
}
holder.name.setText("John Doe");
// etc...
return view;
}
This is the code of the ViewHolder (inline class of PeopleAdapter
):
static class ViewHolder {
@BindView(R.id.name)
TextView name;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
Unfortunately, holder.name.setText("John Doe");
throws a NullPointer exceptions as name
is null. The debug output shows these lines:
13:11:32.816 11613-11613/com.myproject.debug D/ButterKnife: Looking up view binder for com.myproject.controller.adapters.PeopleAdapter$ViewHolder
13:11:32.827 11613-11613/com.myproject.debug D/ButterKnife: Not found. Trying superclass java.lang.Object
13:11:32.827 11613-11613/com.myproject.debug D/ButterKnife: MISS: Reached framework class. Abandoning search.
list_row_people.xml currently looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:padding="4dp"
android:text="@string/example_name"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Reading https://github.com/JakeWharton/butterknife/issues/285, I added the following line to my proguard-rules.pro:
-keep class **$$ViewBinder { *; }
Edit:
Android Studio 2.1 with com.jakewharton:butterknife:8.0.1. This is my build.gradle
:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'kotlin-android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.myproject"
minSdkVersion 18
targetSdkVersion 23
versionCode 18
versionName "0.0.16"
multiDexEnabled true
}
packagingOptions {
exclude 'main/AndroidManifest.xml'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
}
}
lintOptions {
abortOnError false
warning 'InvalidPackage'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
compileOptions {
targetCompatibility JavaVersion.VERSION_1_7
sourceCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
wearApp project(':wear')
testCompile 'junit:junit:4.12'
apt 'com.github.hotchemi:permissionsdispatcher-processor:2.1.2'
releaseCompile project(path: ':common', configuration: 'release')
debugCompile project(path: ':common', configuration: 'debug')
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.maps.android:android-maps-utils:0.4.3'
compile 'com.google.android.gms:play-services-wearable:8.4.0'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.jakewharton.timber:timber:2.5.0'
compile 'com.jakewharton:butterknife:8.0.1'
compile 'com.github.hotchemi:permissionsdispatcher:2.1.2'
compile 'com.squareup.picasso:picasso:2.5.2'
}
buildscript {
ext.kotlin_version = '1.0.1-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
In Butter Knife from Version 8.0.0.
The
Runtime
andcompiler
are now split into two artifacts.
compile 'com.jakewharton:butterknife:8.0.0'
apt 'com.jakewharton:butterknife-compiler:8.0.0'
Look at this Link also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With