无法使用空指针异常错误的 Butterknife

Not working Butterknife with null pointer exception error

我正在尝试在我的 Android 项目中使用 ButterKnife

我有一个 Activity 实现了 Interface。我正在使用 MVP 模式。 我正在尝试从 EditText 获取文本,但它抛出一个

null pointer exception.

这是我的简单代码。

Gradle 依赖项

compile 'com.jakewharton:butterknife:8.8.1'

布局

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.rao.myapplication.LoginActivity">

    <EditText
        android:id="@+id/etUseName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/etUseName"/>
    <Button
        android:id="@+id/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:onClick="loginClick"
        app:layout_constraintTop_toBottomOf="@+id/etPassword"/>

</android.support.constraint.ConstraintLayout>

Activity

 package com.example.rao.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.rao.myapplication.Presenter.LoginActivityPresenter;
import com.example.rao.myapplication.View.LoginActivityView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends AppCompatActivity implements LoginActivityView {

    @BindView(R.id.etUseName)
    EditText userNameET;

    @BindView(R.id.etPassword)
    EditText passwordET;

    @BindView(R.id.btnLogin)
    Button loginBtn;

    private LoginActivityPresenter presenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ButterKnife.bind(this);
        passwordET.setText("abcd"); // Fails here as well
        presenter = new LoginActivityPresenter(this);
    }

    @Override
    public void navigateToListActivity() {

    }

    @Override
    public void loginFailed() {

    }

    public void loginClick(View view) {
        Toast.makeText(this, userNameET.getText().toString(), Toast.LENGTH_SHORT ).show(); // Fails here
        Toast.makeText(this, "test", Toast.LENGTH_SHORT ).show();
    }
}

这是错误日志

FATAL EXCEPTION: main Process: com.example.rao.myapplication, PID: 24847 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rao.myapplication/com.example.rao.myapplication.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference at com.example.rao.myapplication.LoginActivity.onCreate(LoginActivity.java:34) at android.app.Activity.performCreate(Activity.java:6942) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)  at android.app.ActivityThread.-wrap14(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6682)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

请你在这方面指导我正确的方向。

也在您的 gradle 文件中添加以下行....

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.+'//add this line also

希望能解决您的问题....

将其放入 gradle 文件并检查。 这是 official documentation.

的 link

GRADLE

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

如果你忘记在setContentView后面添加ButterKnife.bind(this);,你会得到NullPointerException。