android - NullPointerException在TextInputLayout上

标签 android nullpointerexception butterknife

我是Android开发人员的新手,正在尝试运行旧版应用程序。现在,我面临TextInputLayout的问题。我有以下代码:

public class LoginActivity extends AppCompatActivity {
@Bind(R.id.login_input_user)
TextInputLayout login_input_user;

@Bind(R.id.login_input_password)
TextInputLayout login_input_password;

@Bind(R.id.login_bt_enter)
Button login_bt_enter;

@Bind(R.id.login_version)
TextView login_version;

private String username;
private String password;
private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    mContext = getApplicationContext();

    LoginServices.invalidateAccessToken();

    ButterKnife.bind(this);
    initViews();
}

private void initViews() {
    if (BuildConfig.DEBUG) {
        username = "test";
        password = "test";

        if (login_input_user.getEditText() != null)
            login_input_user.getEditText().setText(username);

        if (login_input_password.getEditText() != null)
            login_input_password.getEditText().setText(password);
    } else if (login_input_user.getEditText() != null) {
        login_input_user.getEditText().setText(LoginServices.getCurrentLogin());
        username = LoginServices.getCurrentLogin();
    }
    new BindValue(login_input_user).setBind(value -> username = value.toString());
    new BindValue(login_input_password).setBind(value -> password = value.toString());

    login_bt_enter.setOnClickListener(v -> {
        Device.hideSoftKeyboard(LoginActivity.this);

        if (isValidLogin()) {
            if (Device.hasNetwork()) {
                doLogin();
            } else {
                CustomAlert.showAlertMessage(LoginActivity.this, R.string.alerta_error, R.string.message_no_network);
            }
        } else {
            CustomAlert.showAlertMessage(LoginActivity.this, R.string.alerta_error, R.string.login_error);
        }
    });

    login_version.setText(String.format(getString(R.string.login_version_text), BuildConfig.BUILD_TYPE, BuildConfig.VERSION_NAME));
}
}

这是我的activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/windowLoginBackground"
    android:padding="8dp"
    android:theme="@style/AppTheme">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/login_logo"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="48dp"
        android:layout_marginTop="48dp"
        android:contentDescription="@string/login_logo_contentdescription"
        android:scaleType="fitEnd"
        android:src="@mipmap/app_logo" />

    <android.support.design.widget.TextInputLayout
        android:id="@+id/login_input_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/login_lb_user"
            android:inputType="textEmailAddress" />
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:id="@+id/login_input_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/login_lb_password"
            android:inputType="textPassword" />
    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/login_bt_enter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:background="@color/colorAccent"
        android:text="@string/login_bt_enter"
        android:textColor="@color/white" />
</LinearLayout>

<TextView
    android:id="@+id/login_version"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:text="Versão: 1.0.0" />

当我点击initViews并获得此行if (login_input_user.getEditText() != null)时,我得到了NullPointerException。看起来login_input_user为null。

我可以在哪里启动?

该代码已经在一段时间前构建并分发。他们使用了以前版本的Android Studio,我不得不将其升级到3.0,并且它的插件必须运行才能运行。因此,这段代码可以正常工作。

最佳答案

不确定是否有帮助,但是我还不能评论问题。

尝试检查您正在使用哪个版本的Butterknife。
如果您还更新了libs版本,则可能会破坏它。

在8版本之后,注释已更改。

@Bind becomes @BindView and @BindViews (one view and multiple views, respectively).



https://github.com/JakeWharton/butterknife/blob/master/CHANGELOG.md

关于android - NullPointerException在TextInputLayout上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47461388/

相关文章:

android - Butterknife 使用 Gradle 插件 3.0 失败

android-gradle-plugin - Android lint AnnotationProcessorOnCompilePath with ButterKnife

java - 尝试导航到 Activity 时主题操作栏出错

java - Google Drive API Android/Java - 文件列表始终为空

Java 在类空间中实例化类数组会产生 NullPointerException

java - NullPointerException LifeGame.java 程序

java - Android 线程详细信息

android - Nearby.Messages 未为此应用启用

java - 我在 “AWT-EventQueue-0”线程java.lang.NullPointerException错误中得到了异常。我该怎么办?

android - 如何在 ButterKnife 中绑定(bind) XML fragment ?