英文:
attempt to invoke virtual method 'android.widget.EditText android.support.design.widget.TextInputLayout.getEditText()' on a null object reference
问题
在Android Studio中出现错误:"java.lang.NullPointerException: 尝试调用虚拟方法 'android.widget.EditText android.support.design.widget.TextInputLayout.getEditText()' 但为空对象引用"。 请帮助我解决这个问题。我已经检查了stackoverflow上的所有解决方案,但无法解决这个问题。
TextInputLayout edtUsername, edtPassword, edtConfirmPassword;
Button btnSignUp;
/* ProgressBar progressBar; */
LinearLayout lvparent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
edtUsername = findViewById(R.id.email_input);
edtPassword = findViewById(R.id.pass_input);
edtConfirmPassword = findViewById(R.id.pass_input_confirm);
btnSignUp = findViewById(R.id.CreateButton);
/* progressBar = findViewById(R.id.pbbar);
lvparent = findViewById(R.id.lvparent); */
this.setTitle("用户注册");
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isEmpty(edtUsername.getEditText().getText().toString()) ||
isEmpty(edtPassword.getEditText().getText().toString()) ||
isEmpty(edtConfirmPassword.getEditText().getText().toString()))
ShowSnackBar("请输入所有字段");
else if (!edtPassword.getEditText().getText().toString().equals(edtConfirmPassword.getEditText().getText().toString()))
ShowSnackBar("密码不匹配");
else {
AddUsers addUsers = new AddUsers();
addUsers.execute("");
}
}
});
}
public void ShowSnackBar(String message) {
Snackbar.make(lvparent, message, Snackbar.LENGTH_LONG)
.setAction("关闭", new View.OnClickListener() {
@Override
public void onClick(View view) {
}
})
.setActionTextColor(getResources().getColor(android.R.color.holo_red_light))
.show();
}
public Boolean isEmpty(String strValue) {
if (strValue == null || strValue.trim().equals(""))
return true;
else
return false;
}
private class AddUsers extends AsyncTask<String, Void, String> {
String name, plainPassword;
@Override
protected void onPreExecute() {
super.onPreExecute();
name = edtUsername.getEditText().getText().toString();
plainPassword = edtPassword.getEditText().getText().toString();
/* progressBar.setVisibility(View.VISIBLE); */
btnSignUp.setVisibility(View.GONE);
}
@Override
protected String doInBackground(String... params) {
try {
ConnectionHelper con = new ConnectionHelper();
Connection connect = ConnectionHelper.CONN();
String queryStmt = "Insert into tblUsers " +
" (UserId,Password,UserRole) values "
+ "('" + name + "','" + plainPassword + "','User')";
PreparedStatement preparedStatement = connect
.prepareStatement(queryStmt);
preparedStatement.executeUpdate();
preparedStatement.close();
return "成功添加";
} catch (SQLException e) {
e.printStackTrace();
return e.getMessage().toString();
} catch (Exception e) {
return "异常。请检查您的代码和数据库。";
}
}
@Override
protected void onPostExecute(String result) {
//Toast.makeText(signup.this, result, Toast.LENGTH_SHORT).show();
ShowSnackBar(result);
/* progressBar.setVisibility(View.GONE); */
btnSignUp.setVisibility(View.VISIBLE);
if (result.equals("成功添加")) {
// Clear();
}
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sign_up_layout"
android:padding="20dp"
android:background="@drawable/sign_up_layout_bg">
<ImageView
android:id="@+id/logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:contentDescription="@string/image_view_des"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:src="@drawable/logo" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_up"
android:textColor="@color/White"
android:typeface="serif"
android:layout_below="@+id/logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="40dp"
android:textSize="24sp"/>
</RelativeLayout>
<android.support.v7.widget.CardView
android:layout_centerHorizontal="true"
android:layout_width="350dp"
android:id="@+id/cardView"
android:layout_marginTop="200dp"
android:layout_height="270dp">
<RelativeLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/full_name_input"
android:hint="@string/username_hint"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="text"
android:textColor="@color/Dark"
android:drawableRight="@drawable/ic_user_blue_24dp"
android:drawableEnd="@drawable/ic_user_blue_24dp"
android:id="@+id/UserEditText"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_below="@id/full_name_input"
android:id="@+id/pass_input"
android:hint="@string/password_hint"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="textPassword"
android:textColor="@color/Dark" />
<!-- android:id="@+id/PassEditText" -->
</android.support.design.widget.TextInputLayout>
<ImageButton
android:id="@+id/pass_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_pass_eye_blue_24dp"
android:layout_alignBottom="@id/pass_input"
android:layout_alignRight="@id/pass_input"
android:layout_alignEnd="@id/pass_input"
android:layout_marginBottom="15dp"
/>
<!-- <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_below="@id/pass_input"
android:id="@+id/email_input"
android:hint="@string/email_hint"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="textEmailAddress"
android:text
<details>
<summary>英文:</summary>
Am getting error in android studio: "java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.EditText android.support.design.widget.TextInputLayout.getEditText()'" on a null object reference". Please help me to resolve this issue. I already check all solution on stackoverflow but could not resolve the issue.
`
TextInputLayout edtUsername, edtPassword, edtConfirmPassword;
Button btnSignUp;
/* ProgressBar progressBar; */
LinearLayout lvparent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
edtUsername = findViewById(R.id.email_input);
edtPassword = findViewById(R.id.pass_input);
edtConfirmPassword = findViewById(R.id.pass_input_confirm);
btnSignUp = findViewById(R.id.CreateButton);
/* progressBar = findViewById(R.id.pbbar);
lvparent = findViewById(R.id.lvparent); */
this.setTitle("User SignUp");
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isEmpty(edtUsername.getEditText().getText().toString()) ||
isEmpty(edtPassword.getEditText().getText().toString()) ||
isEmpty(edtConfirmPassword.getEditText().getText().toString()))
ShowSnackBar("Please enter all fields");
else if (!edtPassword.getEditText().getText().toString().equals(edtConfirmPassword.getEditText().getText().toString()))
ShowSnackBar("Password does not match");
else {
AddUsers addUsers = new AddUsers();
addUsers.execute("");
}
}
});
}
public void ShowSnackBar(String message) {
Snackbar.make(lvparent, message, Snackbar.LENGTH_LONG)
.setAction("CLOSE", new View.OnClickListener() {
@Override
public void onClick(View view) {
}
})
.setActionTextColor(getResources().getColor(android.R.color.holo_red_light))
.show();
}
public Boolean isEmpty(String strValue) {
if (strValue == null || strValue.trim().equals(("")))
return true;
else
return false;
}
private class AddUsers extends AsyncTask<String, Void, String> {
String name, plainPassword;
@Override
protected void onPreExecute() {
super.onPreExecute();
name = edtUsername.getEditText().getText().toString();
plainPassword = edtPassword.getEditText().getText().toString();
/* progressBar.setVisibility(View.VISIBLE); */
btnSignUp.setVisibility(View.GONE);
}
@Override
protected String doInBackground(String... params) {
try {
ConnectionHelper con = new ConnectionHelper();
Connection connect = ConnectionHelper.CONN();
String queryStmt = "Insert into tblUsers " +
" (UserId,Password,UserRole) values "
+ "('"
+ name
+ "','"
+ plainPassword
+ "','User')";
PreparedStatement preparedStatement = connect
.prepareStatement(queryStmt);
preparedStatement.executeUpdate();
preparedStatement.close();
return "Added successfully";
} catch (SQLException e) {
e.printStackTrace();
return e.getMessage().toString();
} catch (Exception e) {
return "Exception. Please check your code and database.";
}
}
@Override
protected void onPostExecute(String result) {
//Toast.makeText(signup.this, result, Toast.LENGTH_SHORT).show();
ShowSnackBar(result);
/* progressBar.setVisibility(View.GONE); */
btnSignUp.setVisibility(View.VISIBLE);
if (result.equals("Added successfully")) {
// Clear();
}
}
}
`
`
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sign_up_layout"
android:padding="20dp"
android:background="@drawable/sign_up_layout_bg">
<ImageView
android:id="@+id/logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:contentDescription="@string/image_view_des"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:src="@drawable/logo" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_up"
android:textColor="@color/White"
android:typeface="serif"
android:layout_below="@+id/logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="40dp"
android:textSize="24sp"/>
</RelativeLayout>
<android.support.v7.widget.CardView
android:layout_centerHorizontal="true"
android:layout_width="350dp"
android:id="@+id/cardView"
android:layout_marginTop="200dp"
android:layout_height="270dp">
<RelativeLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/full_name_input"
android:hint="@string/username_hint"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="text"
android:textColor="@color/Dark"
android:drawableRight="@drawable/ic_user_blue_24dp"
android:drawableEnd="@drawable/ic_user_blue_24dp"
android:id="@+id/UserEditText"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_below="@id/full_name_input"
android:id="@+id/pass_input"
android:hint="@string/password_hint"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="textPassword"
android:textColor="@color/Dark" />
<!-- android:id="@+id/PassEditText" -->
</android.support.design.widget.TextInputLayout>
<ImageButton
android:id="@+id/pass_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_pass_eye_blue_24dp"
android:layout_alignBottom="@id/pass_input"
android:layout_alignRight="@id/pass_input"
android:layout_alignEnd="@id/pass_input"
android:layout_marginBottom="15dp"
/>
<!-- <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_below="@id/pass_input"
android:id="@+id/email_input"
android:hint="@string/email_hint"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="textEmailAddress"
android:textColor="@color/Dark"
android:drawableRight="@drawable/ic_email_blue_24dp"
android:drawableEnd="@drawable/ic_email_blue_24dp"
android:id="@+id/EmailEditText"/>
</android.support.design.widget.TextInputLayout> -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_below="@id/pass_input"
android:id="@+id/pass_input_confirm"
android:hint="@string/password_hint"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="textPassword"
android:textColor="@color/Dark" />
<!-- android:id="@+id/PassEditText1" -->
</android.support.design.widget.TextInputLayout>
<ImageButton
android:id="@+id/pass_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_pass_eye_blue_24dp"
android:layout_alignBottom="@id/pass_input_confirm"
android:layout_alignRight="@id/pass_input"
android:layout_alignEnd="@id/pass_input"
android:layout_marginBottom="15dp"
/>
<!-- <CheckBox
android:id="@+id/ageCheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/pass_input"
android:layout_alignStart="@id/pass_input"
android:textColor="@color/dimGray"
android:textStyle="bold"
android:layout_below="@id/pass_input_confirm"
android:text="@string/age_checkbox"/> -->
</RelativeLayout>
</android.support.v7.widget.CardView>
<Button
style="@android:style/Widget.Holo.Button.Small"
android:id="@+id/CreateButton"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@id/sign_up_layout"
android:layout_centerHorizontal="true"
android:layout_marginTop="202dp"
android:text="@string/sign_up"
android:textColor="@color/White"
android:background="@drawable/sign_up_bg"/>
<TextView
android:id="@+id/TermsTextView"
android:layout_below="@id/CreateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/terms_text_view_text"
android:textColor="@color/LightGray"
android:maxLines="2"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:gravity="center"
/>
</RelativeLayout>
`
</details>
# 答案1
**得分**: 0
只保留`EditText`视图,移除`TextInputLayout`。问题出在您在`TextInputLayout`上使用`getEditText()`,它是`EditText`的`child()`,因此导致了空指针异常。
<details>
<summary>英文:</summary>
Remove `TextInputLayout` and just keep `EditText` view. The problem is because you are using the `getEditText()` which is a `child()` of `EditText` on `TextInputLayout`. Hence, gettign nullpointer exception.
</details>
# 答案2
**得分**: 0
将这些id设置为`EditText`,而不是`TextInputLayout`,并相应地更改您的代码如下所示:
```java
EditText edtUsername, edtPassword, edtConfirmPassword;
@Override
public void onClick(View v) {
if (isEmpty(edtUsername.getText().toString()) ||
isEmpty(edtPassword.getText().toString()) ||
isEmpty(edtConfirmPassword.getText().toString()))
ShowSnackBar("请输入所有字段");
else if (!edtPassword.getText().toString().equals(edtConfirmPassword.getText().toString()))
ShowSnackBar("密码不匹配");
else {
AddUsers addUsers = new AddUsers();
addUsers.execute("");
}
}
请注意,我已将双引号内的文本从HTML实体("
)转换为普通文本。
英文:
Set those id email_input
, pass_input
and pass_input_confirm
to EditText
instead of TextInputLayout
and change your code accordingly like below:
EditText edtUsername, edtPassword, edtConfirmPassword;
@Override
public void onClick(View v) {
if (isEmpty(edtUsername.getText().toString()) ||
isEmpty(edtPassword.getText().toString()) ||
isEmpty(edtConfirmPassword.getText().toString()))
ShowSnackBar("Please enter all fields");
else if (!edtPassword.getText().toString().equals(edtConfirmPassword.getText().toString()))
ShowSnackBar("Password does not match");
else {
AddUsers addUsers = new AddUsers();
addUsers.execute("");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论