Butter Knife return null pointer

风流意气都作罢 提交于 2019-12-05 11:47:53

问题


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


回答1:


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'



回答2:


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.




回答3:


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 with kapt.

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'



回答4:


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.




回答5:


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"




回答6:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!