黄油刀返回空指针 [英] Butter Knife return null pointer

查看:69
本文介绍了黄油刀返回空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中使用黄油刀,我根据黄油刀教程做了一切.但是,当我为视图设置任何内容(setText,setClickListener ...)时,我得到了空对象引用异常.

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.

这是我的代码:

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

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>

错误日志

 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

我的代码有什么问题?

谢谢

推荐答案

我已修复它.

我的build.gradle有问题

There is a problem with my build.gradle

我忘记添加

apt 'com.jakewharton:butterknife-compiler:8.0.1'

到build.gradle

to the build.gradle

谢谢大家

更新

如果您使用的是 neenbedankt.android-apt 插件,请先将其删除.

If you are using neenbedankt.android-apt plugin first remove it.

然后删除 apt'com.jakewharton:butterknife-compiler:8.0.1'

然后添加 annotationProcessor'com.jakewharton:butterknife-compiler:8.8.1'到依赖项.

更新2

如果您使用的是Kotlin,请替换:

If you are using kotlin replace :

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

具有:

kapt 'com.jakewharton:butterknife-compiler:8.8.1'

别忘了添加

apply plugin: 'kotlin-kapt'

之后:

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

这篇关于黄油刀返回空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆