I want to using Butter Knife in my project.I did everything according to the Butter Knife tutorial. But when I set anything to the views (setText, setClickListener ...) I got null object reference exception.
This is my code:
public class LoginActivity extends AppCompatActivity implements LoginView, View.OnClickListener {
@BindView(R.id.acEtUsername) AppCompatEditText userName;
@BindView(R.id.acEtPassword) AppCompatEditText password;
@BindView(R.id.prgCheckLogin) ProgressBar prgCheckLogin;
@BindView(R.id.btnLogin) Button btnLogin;
LoginPresenter loginPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
ButterKnife.setDebug(true);
loginPresenter = new LoginPresenterImpl(this);
btnLogin.setOnClickListener(this); // or userName.setText("userName");
}
/** Other Methods **/
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/acEtUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginRight="32dp"
android:layout_marginLeft="32dp"
android:hint="@string/user_name"/>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/acEtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="8dp"
android:layout_marginRight="32dp"
android:layout_marginLeft="32dp"
android:hint="@string/password"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
android:text="@string/login"/>
<ProgressBar
android:id="@+id/prgCheckLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_gravity="center|bottom"/>
</LinearLayout>
And error log
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.AppCompatEditText.setText(java.lang.CharSequence)' on a null object reference
What's wrong in my code ?
Thanks
I fixed it.
There is a problem with my build.gradle
I forgot to add
apt 'com.jakewharton:butterknife-compiler:8.0.1'
to the build.gradle
Thank everyone
UPDATE
If you are using neenbedankt.android-apt
plugin first remove it.
Then remove apt 'com.jakewharton:butterknife-compiler:8.0.1'
And then add annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
to the dependencies.
UPDATE 2
If you are using kotlin replace :
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
with:
kapt 'com.jakewharton:butterknife-compiler:8.8.1'
And don't forget to add
apply plugin: 'kotlin-kapt'
after:
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
In your onCreate
method, make sure you have the line:
ButterKnife.bind(this);
Without that line, the binds you set up aren't performed, and the views remain null.
Use the following if your are using the new Butter Knife version:
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
If you are using Kotlin, replace
annotationProcessor
withkapt
.
UPDATE:
If you are using Gradle plugin 3.0 or above in your project, change compile
to implementation
. like below:
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
Yep, Butterknife by Jake Wharton has been updated to 8.0.1
Please refer at his git account for steps Butterknife Git
On final note : Make sure the line apply plugin ... is placed somewhere at the top of the file.
I started getting NPE errors when, on an existing project, I added support for DataBinding and Kotlin.
I had:
annotationProcessor 'com.jakewharton:butterknife-compiler:x.x.x'
..and replaced with:
kapt "com.jakewharton:butterknife-compiler:x.x.x"
I got the same exception. In my case I forgot to add jcenter()
repository in my app-module build.gradle
file.
buildscript {
repositories {
mavenCentral()
//this was missed
jcenter()
}
dependencies {
...
}
}
来源:https://stackoverflow.com/questions/36925765/butter-knife-return-null-pointer