我希望有人可以帮助我。下面是我的创建/注册用户的代码。我使用的是Android Studio和Firebase。出于某种原因,代码不是创建新用户。我可以手动将用户添加到数据库,但是,当我运行模拟器并测试登录时,我无法创建新用户。该程序在运行progressDialog时遇到困难。当我删除progressDialog时,我没有得到任何响应,因此当调用createUserWithEmailAndPassword()时程序似乎被卡住了。我在Firebase控制台中启用了电子邮件和密码身份验证。我不确定问题是什么,并希望得到更熟练编码的人的见解。提前谢谢大家。
public class RegisterPage extends AppCompatActivity implements View.OnClickListener{
//declaration of views (variables)
private Button btn_signup;
private EditText txt_firstname;
private EditText txt_lastname;
private EditText txt_email_signup;
private EditText txt_username;
private EditText txt_password_signup;
private EditText txt_passwordConfirm;
private ProgressDialog progressDialog;
private FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthStateListener;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_page);
//initialization of view (assign id's)
progressDialog = new ProgressDialog(this);
btn_signup = (Button) findViewById(R.id.btn_signup);
txt_firstname = (EditText) findViewById(R.id.txt_firstname);
txt_lastname = (EditText) findViewById(R.id.txt_lastname);
txt_email_signup = (EditText) findViewById(R.id.txt_email_signup);
txt_username = (EditText) findViewById(R.id.txt_username);
txt_password_signup = (EditText) findViewById(R.id.txt_password_signup);
txt_passwordConfirm = (EditText)findViewById(R.id.txt_passwordConfirm);
//assign database instances
mAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user != null) {
}
else{
startActivity(new Intent(RegisterPage.this, UserMainPage.class));
}
}
};
//set the listener for the click event
btn_signup.setOnClickListener(this);
}
//function to register user
private void registerUser(){
//get user input
String email = txt_email_signup.getText().toString().trim();
String password = txt_password_signup.getText().toString().trim();
String confirm = txt_passwordConfirm.getText().toString().trim();
String firstname = txt_firstname.getText().toString().trim();
String lastname = txt_lastname.getText().toString().trim();
String username = txt_username.getText().toString().trim();
//check if stings are empty using TextUtils
if(TextUtils.isEmpty(firstname)){ //email is empty
Toast.makeText(this, "Please enter firstname", Toast.LENGTH_SHORT).show();
//stop further execution
return;
}
if(TextUtils.isEmpty(lastname)){ //email is empty
Toast.makeText(this, "Please enter lastname", Toast.LENGTH_SHORT).show();
//stop further execution
return;
}
if(TextUtils.isEmpty(username)){ //email is empty
Toast.makeText(this, "Please enter a username", Toast.LENGTH_SHORT).show();
//stop further execution
return;
}
if(TextUtils.isEmpty(email)){ //email is empty
Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
//stop further execution
return;
}
if(TextUtils.isEmpty(password)){ //password is empty
Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
//stop further execution
return;
}
if(!password.equals(confirm)){
Toast.makeText(this, "Your passwords do not match", Toast.LENGTH_SHORT).show();
//stop further execution
return;
}
//if validations are okay
//we will show a progressDialog as we create user account
progressDialog.setMessage("Creating account...");
progressDialog.show();
//register user in firebase database
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if (task.isSuccessful()){
// user registered, start profile activity
Toast.makeText(RegisterPage.this,"Account Created",Toast.LENGTH_LONG).show();
finish();
startActivity(new Intent(getApplicationContext(), UserMainPage.class));
}
else{
Toast.makeText(RegisterPage.this,"Could not create account. Please try again",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onClick(View view){
if(view == btn_signup){
//if signup button clicked call function register user
registerUser();
}
}
/* @Override
protected void onStart(){
super.onStart();
mAuth.addAuthStateListener(mAuthStateListener);
}
@Override
protected void onStop(){
super.onStop();
mAuth.removeAuthStateListener(mAuthStateListener);
}*/
}
答案 0 :(得分:1)
这种情况正在发生,因为您根本就没有创建用户。因此,在onComplete
方法中,您需要获得Firebase
当前用户。所以if (task.isSuccessful()
让用户这样:
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if (task.isSuccessful()){
FirebaseUser user = mAuth.getCurrentUser(); //You Firebase user
// user registered, start profile activity
Toast.makeText(RegisterPage.this,"Account Created",Toast.LENGTH_LONG).show();
finish();
startActivity(new Intent(getApplicationContext(), UserMainPage.class));
}
else{
Toast.makeText(RegisterPage.this,"Could not create account. Please try again",Toast.LENGTH_SHORT).show();
}
}
});
希望它有所帮助。