Android TextInputLayout上的NullPointerException
我是Android开发者的新手,我正在尝试运行一个遗留应用程序。现在我面临着文本输入布局的问题。我有以下代码:Android TextInputLayout上的NullPointerException,android,nullpointerexception,butterknife,Android,Nullpointerexception,Butterknife,我是Android开发者的新手,我正在尝试运行一个遗留应用程序。现在我面临着文本输入布局的问题。我有以下代码: 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; @Bi
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));
}
}
这是我的活动_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\u input\u user.getEditText()!=null)
我得到了一个NullPointerException。看起来登录\输入\用户为空
我在哪里可以开始
这段代码在一段时间前就已经构建和发布了。他们使用了以前版本的Android Studio,我不得不将其升级到3.0,并将其插件升级到必要的版本才能运行。所以这段代码曾经运行得很好。试试这个
if (login_input_user.getEditText().toString() != null)
或
不确定这是否有帮助,但我还不能对问题发表评论 尝试检查您正在使用的Butterknife版本。
如果您还更新了libs版本,它可能会破坏它 在8版本之后,注释被更改 @Bind变成@BindView和@BindView(一个视图和多个视图, 分别)
将
activity\u login.xml
添加到问题中。请将您的xml文件粘贴到此处。id是否正确?我用xml文件编辑了帖子。请检查我的Answeet更新,但遇到了相同的问题。请确定。现在您有:依赖项{compile'com.jakewharton:butterknife:8.8.1'annotationProcessor'com.jakewharton:butterknife compiler:8.8.1'}
和所有用@BindView(…)
注释的视图?我使用的是8.4.0,没有使用annotationProcessor。我的错!先生,你救了我的命!谢谢
if(login_input_user.getEditText().toString().trim().length() == 0)