由于出现以下错误并且我的新用户注册未添加到Firebase中,我需要您的帮助。
我不确定我是否错过了编码。
public class MainActivity extends AppCompatActivity {
Button btnSignIn, btnSignUp;
RelativeLayout rootLayout;
FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Arkhip_font.ttf")
.setFontAttrId(R.attr.fontPath)
.build());
setContentView(R.layout.activity_main);
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
users = db.getReference("Users");
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignIn = (Button) findViewById(R.id.btnSignIn);
rootLayout = (RelativeLayout) findViewById(R.id.rootLayout) ;
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showRegisterDialog();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showLoginDialog();
}
});
}
private void showLoginDialog() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Sign In");
dialog.setMessage("Please use email to sign in");
LayoutInflater inflater = LayoutInflater.from(this);
View login_layout = inflater.inflate(R.layout.layout_login,null);
final MaterialEditText edtEmail = login_layout.findViewById(R.id.edtEmail);
final MaterialEditText edtPassword = login_layout.findViewById(R.id.edtPassword);
dialog.setView(login_layout);
dialog.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (TextUtils.isEmpty(edtEmail.getText().toString())) {
Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_SHORT)
.show();
return;
}
if (TextUtils.isEmpty(edtPassword.getText().toString())) {
Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_SHORT)
.show();
return;
}
if (edtPassword.getText().toString().length() < 6) {
Snackbar.make(rootLayout, "Password too short!", Snackbar.LENGTH_SHORT)
.show();
return;
}
auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
startActivity(new Intent(MainActivity.this,Welcome.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT)
.show();
}
});
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
private void showRegisterDialog() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Sign Up");
dialog.setMessage("Please use email to sign up");
LayoutInflater inflater = LayoutInflater.from(this);
View register_layout = inflater.inflate(R.layout.layout_signup,null);
final MaterialEditText edtEmail = register_layout.findViewById(R.id.edtEmail);
final MaterialEditText edtPassword = register_layout.findViewById(R.id.edtPassword);
final MaterialEditText edtName = register_layout.findViewById(R.id.edtName);
final MaterialEditText edtNRIC = register_layout.findViewById(R.id.edtNRIC);
final MaterialEditText edtAddress = register_layout.findViewById(R.id.edtAddress);
final MaterialEditText edtMobile = register_layout.findViewById(R.id.edtMobile);
dialog.setView(register_layout);
dialog.setPositiveButton("Sign Up", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if(TextUtils.isEmpty(edtEmail.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter email address",Snackbar.LENGTH_SHORT)
.show();
return;
}
if(TextUtils.isEmpty(edtMobile.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter phone number",Snackbar.LENGTH_SHORT)
.show();
return;
}
if(TextUtils.isEmpty(edtName.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter name",Snackbar.LENGTH_SHORT)
.show();
return;
}
if(TextUtils.isEmpty(edtAddress.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter address",Snackbar.LENGTH_SHORT)
.show();
return;
}
if(TextUtils.isEmpty(edtNRIC.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter 12 digits NRIC",Snackbar.LENGTH_SHORT)
.show();
return;
}
if(TextUtils.isEmpty(edtPassword.getText().toString()))
{
Snackbar.make(rootLayout,"Please enter password",Snackbar.LENGTH_SHORT)
.show();
return;
}
if(edtPassword.getText().toString().length()<6)
{
Snackbar.make(rootLayout,"Password too short!",Snackbar.LENGTH_SHORT)
.show();
return;
}
auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
User user = new User();
user.setEmail(edtEmail.getText().toString());
user.setPassword(edtPassword.getText().toString());
user.setName(edtName.getText().toString());
user.setNRIC(edtNRIC.getText().toString());
user.setAddress(edtAddress.getText().toString());
user.setMobile(edtMobile.getText().toString());
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout,"Sign up successfully!",Snackbar.LENGTH_SHORT)
.show();
return;
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Sign up failed!"+e.getMessage(),Snackbar.LENGTH_SHORT)
.show();
return;
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout,"Sign up failed!"+e.getMessage(),Snackbar.LENGTH_SHORT)
.show();
return;
}
});
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();