Android Firebase 创建新用户不工作

Android Firebase create new user not working

我希望有人能帮助我。下面是我的 create/register 用户的代码。我正在使用 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);
 }*/
}

发生这种情况是因为您根本没有创建用户。因此,在您的 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();
        }
    }
    }); 

希望对您有所帮助。