首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NullPointerException on TextInputLayout

NullPointerException on TextInputLayout
EN

Stack Overflow用户
提问于 2017-11-23 18:11:31
回答 2查看 445关注 0票数 1

我刚接触过Android,我正在尝试运行一个遗留应用程序。现在,我面临着TextInputLayout的一个问题。我有以下代码:

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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,我不得不将它升级到3.0,并将它的插件升级到运行所需的版本。所以这段代码曾经很好用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-23 18:42:53

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

试着检查一下你使用的是哪个版本的蝴蝶刀。

如果你也更新了libs版本,它可能会破坏它。

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

@BindView变成@BindView和@BindViews (分别是一个视图和多个视图)。

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

票数 1
EN

Stack Overflow用户

发布于 2017-11-23 18:33:33

尝尝这个

代码语言:javascript
复制
if (login_input_user.getEditText().toString() != null)

代码语言:javascript
复制
if(login_input_user.getEditText().toString().trim().length() == 0)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47461388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档