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'