gpt4 book ai didi

android - butterknife 返回空指针

转载 作者:行者123 更新时间:2023-12-03 14:27:44 24 4
gpt4 key购买 nike

我想在我的项目中使用 butterknife 。我按照 butterknife 教程做了一切。
但是当我为 View 设置任何东西(setText、setClickListener ...)时,我得到了空对象引用异常。

这是我的代码:

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 登录.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 有问题

我忘了添加

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

到 build.gradle

谢谢大家

更新

如果您使用 neenbedankt.android-apt插件首先删除它。

然后删除 apt 'com.jakewharton:butterknife-compiler:8.0.1'
然后添加 annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
到依赖项。

更新 2

如果您使用的是 kotlin 替换:
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'

关于android - butterknife 返回空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36925765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com