首页 文章

不工作Butterknife与空指针异常错误

提问于
浏览
1

我想在我的Android项目中使用 ButterKnife .

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

空指针异常 .

这是我的简单代码 .

Gradle dependencies

compile 'com.jakewharton:butterknife:8.8.1'

Layout

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

Here is the error log

FATAL EXCEPTION:main进程:com.example.rao.myapplication,PID:24847 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.rao.myapplication / com.example.rao.myapplication.LoginActivity}:java .lang.NullPointerException:尝试在android上的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)的空对象引用上调用虚方法'void android.widget.EditText.setText(java.lang.CharSequence)' . app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)位于android.app.Handler上android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1631)的android.app.ActivityThread.-wrap14(ActivityThread.java) . dispatchMessage(Handler.java:102)位于android.app.Looper.loop(Looper.java:154)的android.app.ActivityThread.main(ActivityThread.java:6682),位于java.lang.reflect.Method.invoke(Native方法)在com.android.internal.os.ZygoteInit.main(Zygo)com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520) teInit.java:1410)引起:java.lang.NullPointerException:尝试在com.example.rao.myapplication上的空对象引用上调用虚方法'void android.widget.EditText.setText(java.lang.CharSequence)' .LoginActivity.onCreate(LoginActivity.java:34)位于android.app.A.运行中的android.app.Activity.hactCaate.performLaunchActivity上的android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)上的android.app.Activity.performCreate(Activity.java:6942) ActivityThread.java:2880)在android.app.A活动中的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)android.app.ActivityThread $ H.handleMessage(ActivityThread.java)的android.app.ActivityThread.-wrap14(ActivityThread.java) 1631)在Android.os.Handler.dispatchMessage(Handler.java:102)的android.app.Looper.loop(Looper.java:154)在android.app.ActivityThread.main(ActivityThread.java:6682)在java . com.android.inter上的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)中的lang.reflect.Method.invoke(Native Method) nal.os.ZygoteInit.main(ZygoteInit.java:1410)

请你指点我正确的方向 .

2 回答

  • 6

    add below line also in your gradle file....

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

    Hope it solve your problems....

  • 2

    把它放在gradle文件中并检查 . 这是official documentation.的链接

    GRADLE

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

相关问题