java - 尝试调用虚拟方法 'android.text.Editable android.widget.EditText.getText()'
问题描述
我使用 firebase 数据库来注册具有自定义字段的用户。突然我得到这个意外的错误。我之前没有看到这个错误。
致命异常:主进程:com.example.muhammadtehmoor.testing,PID:23770 java.lang.NullPointerException:尝试在空对象引用 com 上调用虚拟方法 android.text.Editable android.widget.EditText.getText()'。 example.muhammadtehmoor.testing SignUpActivity$1 ..onClick(SignUpActivity.java:49)
这是我的注册活动:
package com.example.muhammadtehmoor.testing;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class SignUpActivity extends AppCompatActivity {
EditText txtname,txtphone,txtemail,txtpassword;
String fullname="",email="",phone="",password = "";
Button submit,cancel;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
private FirebaseDatabase firebaseDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
txtname = (EditText) findViewById(R.id.edittext2);
txtphone = (EditText)findViewById(R.id.editText3);
txtemail = (EditText)findViewById(R.id.editText4);
txtpassword = (EditText)findViewById(R.id.editText5);
submit = (Button)findViewById(R.id.btn_submit);
cancel = (Button)findViewById(R.id.btn_cancel);
databaseReference = FirebaseDatabase.getInstance().getReference("Driver");
firebaseAuth = FirebaseAuth.getInstance();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fullname = txtname.getText().toString();
email = txtemail.getText().toString();
phone = txtphone.getText().toString();
password = txtpassword.getText().toString();
if(TextUtils.isEmpty(fullname))
{
txtname.setError("Enter a valid credential");
txtname.requestFocus();
return;
}
if(TextUtils.isEmpty(email))
{
txtemail.setError("Enter a valid credential");
txtemail.requestFocus();
return;
}
if(TextUtils.isEmpty(phone))
{
txtphone.setError("Enter a valid credential");
txtphone.requestFocus();
return;
}
if(TextUtils.isEmpty(password))
{
txtpassword.setError("Enter a valid credential");
txtpassword.requestFocus();
return;
}
if(password.length()<6)
{
txtpassword.setError("password too short");
txtpassword.requestFocus();
return;
}
firebaseAuth.createUserWithEmailAndPassword(txtemail.getText().toString(), txtpassword.getText().toString())
.addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Driver object = new Driver(
fullname,
phone,
email
);
FirebaseDatabase.getInstance().getReference("Driver")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(object).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// next.setVisibility(View.GONE);
//back.setVisibility(View.GONE);
Toast.makeText(SignUpActivity.this, "Registeration Complete", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignUpActivity.this, LoginActivity.class));
} else {
Toast.makeText(SignUpActivity.this, "Account creation failed", Toast.LENGTH_SHORT).show();
}
}
});
//updateUserInfo(pickedImageUri,firebaseAuth.getCurrentUser());
} else {
Toast.makeText(SignUpActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
// ...
}
});////firebase auth
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
这是我的注册 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.muhammadtehmoor.testing.SignUpActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Sign Up Form"
android:textSize="35dp"
android:gravity="center"
android:textColor="@android:color/black"/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter your Name" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:ems="10"
android:inputType="phone"
android:hint="Enter your phone no" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:ems="10"
android:inputType="textEmailAddress"
android:hint="Enter your email" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:passwordToggleEnabled="true"
app:hintEnabled="true"
app:passwordToggleDrawable="@drawable/passwordlock">
<EditText
android:id="@+id/editText5"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:ems="10"
android:inputType="textPassword"
android:hint="Enter your password" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btn_submit"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Submit"
android:background="@drawable/button_design"
android:layout_marginLeft="100dp"
android:drawableRight="@drawable/ic_send_black_24dp"
android:drawableTint="@color/colorPrimaryDark"
android:paddingRight="10dp"
android:layout_marginTop="20dp"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:background="@drawable/button_design"
android:drawableRight="@drawable/ic_cancel_black_24dp"
android:drawableTint="@color/colorPrimaryDark"
android:paddingRight="10dp"
android:text="Cancel"
android:textColor="@android:color/black"
tools:ignore="InvalidId" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
解决方案
请更换
txtname = (EditText) findViewById(R.id.edittext2);
至
txtname = (EditText) findViewById(R.id.editText2);
您已经在 XML 中使用大写 T 声明了 id,并且在 java 中使用了小写 t。
推荐阅读
- c# - 列表的通用扩展方法
不删除空值 - javascript - 我不能将 var 设置为 window.screenY
- c - 请用 libcsv 提供建议。我只需要阅读第一行
- node.js - Sequelize - 使用 AND 运算符连接查询表
- .net - 在 VB.net 网络浏览器中嵌入 Excel 实例
- excel - VBA to click on a button on an IE form to submit it
- docker - How to access to a created docker image
- r - 有没有办法在R中密码保护多个excel文件
- asp.net-core - 使用 EF Core 通常查找和设置主键的正确方法是什么
- javascript - Postman Pre-Request Script to create JWT for Google Service Account