TextInputLayout 上的 NullPointerException

NullPointerException on TextInputLayout

我是 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 为空。

我在哪里可以启动这个?

此代码已在一段时间前构建和分发。他们使用以前版本的 Android Studio,我不得不将其升级到 3.0,并且它是 运行 必要版本的插件。所以这段代码曾经工作得很好。

试试这个

if (login_input_user.getEditText().toString() != null)

if(login_input_user.getEditText().toString().trim().length() == 0)

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

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

8版本后,注释发生了变化

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

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