Reputation: 2136
I am using Butterknife for my android project. I have previously used this, but for some reason it no longer works with the new update.
Here is my setup:
(App Module) build.gradle:
compile 'com.jakewharton:butterknife:8.4.0'
activity:
@BindView(R.id.navigation_pager) ViewPager mViewPager;
/.../
@Override
protected void onCreate(Bundle savedInstanceState) {
/...
/...
ButterKnife.bind(this);
stacktrace:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
What am I missing in order to get Butterknife
working?
Thanks.
Upvotes: 0
Views: 487
Reputation: 14053
Configure your project-level build.gradle to include the 'android-apt' plugin:
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.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
.`
class ExampleActivity extends Activity {
@BindView(R.id.navigation_pager) ViewPager mViewPager;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
viewPager.setAdapter(new CustomPagerAdapter(this));
}
}
**MAKE SURE TO CALL setAdapter() only after ButterKnife.bind(this);**
Upvotes: 1
Reputation: 12379
You need to add:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
in Project gradle file
And Add:
apt 'com.jakewharton:butterknife-compiler:8.4.0'
and this at top:
apply plugin: 'android-apt'
for butterknife to work
For more info refer this link:https://github.com/JakeWharton/butterknife
Upvotes: 5