没有工作Butterknife与空指针异常错误

时间:2017-10-06 05:42:26

标签: android butterknife

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

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

  

空指针异常。

这是我的简单代码。

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>

活动

 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();
    }
}

这是错误日志

  

致命的例外:主要                                                      处理:com.example.rao.myapplication,PID:24847                                                      java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.rao.myapplication / com.example.rao.myapplication.LoginActivity}:   java.lang.NullPointerException:尝试调用虚方法'void   在null上的android.widget.EditText.setText(java.lang.CharSequence)'   对象参考                                                          在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)                                                          在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)                                                          在android.app.ActivityThread.-wrap14(ActivityThread.java)                                                          在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1631)                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                          在android.os.Looper.loop(Looper.java:154)                                                          在android.app.ActivityThread.main(ActivityThread.java:6682)                                                          at java.lang.reflect.Method.invoke(Native Method)                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)                                                       引起:java.lang.NullPointerException:尝试调用虚方法'void   在null上的android.widget.EditText.setText(java.lang.CharSequence)'   对象参考                                                          在com.example.rao.myapplication.LoginActivity.onCreate(LoginActivity.java:34)                                                          在android.app.Activity.performCreate(Activity.java:6942)                                                          在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)                                                          在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)                                                          在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)                                                          在android.app.ActivityThread.-wrap14(ActivityThread.java)                                                          在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1631)                                                          在android.os.Handler.dispatchMessage(Handler.java:102)                                                          在android.os.Looper.loop(Looper.java:154)                                                          在android.app.ActivityThread.main(ActivityThread.java:6682)                                                          at java.lang.reflect.Method.invoke(Native Method)                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

请你指点我正确的方向。

3 个答案:

答案 0 :(得分:6)

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

/home/bot/node_modules/log4js/lib/log4js.js:25
const debug = require('debug')('log4js:main');
^^^^^
SyntaxError: Use of const in strict mode.
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/home/bot/site.js:2:14)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)

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

答案 1 :(得分:2)

将它放在gradle文件中并检查。 以下是official documentation.

的链接

<强>摇篮

{ "param": "value" }

答案 2 :(得分:0)

如果您忘记在setContentView之后添加ButterKnife.bind(this);,则会收到NullPointerException。