Firebase createUser使用电子邮件和密码任务.isSuccessful()

我正在创建一个安卓应用程序,并正在实现登录/注册功能。

我正处于注册活动在我的Firebase应用程序中成功创建用户条目的阶段,但是,我似乎无法跟踪任务是否成功。

http://img4.mukewang.com/63218c8700017a2906560341.jpg

private void startRegister() {

    String email = mEmailField.getText().toString();

    String password = mPasswordField.getText().toString();

    String confirmPassword = mConfirmPassword.getText().toString();


    // Check that fields are not empty

    if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {


        Toast.makeText(Register.this, "Email, password or confirm password field cannot be empty.", Toast.LENGTH_LONG).show();

    } else if (!password.equals(confirmPassword)) {


        Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();

    } else {


        mAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {

            @Override

            public void onSuccess(AuthResult authResult) {


                Toast.makeText(Register.this, "Success", Toast.LENGTH_LONG).show();

            }

        }).addOnFailureListener(new OnFailureListener() {

            @Override

            public void onFailure(@NonNull Exception e) {


                Toast.makeText(Register.this, "Failure", Toast.LENGTH_LONG).show();

            }

        });

    }

}

如果 !task.是成功的() 或者块曾经被到达,但用户是在火库中创建的。任何想法为什么我不能跟踪成功/如果它失败了?


相比之下:


这在我的登录类中起作用。


        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {

            @Override

            public void onComplete(@NonNull Task<AuthResult> task) {


                if (!task.isSuccessful()) {

                    Toast.makeText(Login.this, "Credentials error, user may not exist.", Toast.LENGTH_LONG).show();

                }

            }

        });


慕无忌1623718
浏览 147回答 2
2回答

素胚勾勒不出你

很难说目前的实施方式是怎么回事。尝试直接添加成功mAuth.createUserWithEmailAndPassword(email, pass).addOnSuccessListener(new OnSuccessListener<AuthResult>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onSuccess(AuthResult authResult) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//done&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }).addOnFailureListener(new OnFailureListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(@NonNull Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //display toast if registering failed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ToastRect.failed(RegisterActivity.this, getString(R.string.app_activities_error_text)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;});

RISEBY

public class Register extends AppCompatActivity {private EditText mEmailField;private EditText mPasswordField;private EditText mConfirmPassword;private Button mRegisterButton;private FirebaseAuth mAuth;private FirebaseAuth.AuthStateListener mAuthListener;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; FirebaseApp.initializeApp(this);&nbsp; &nbsp; setContentView(R.layout.activity_register);&nbsp; &nbsp; mEmailField = findViewById(R.id.registerEmailField);&nbsp; &nbsp; mPasswordField = findViewById(R.id.registerPasswordField);&nbsp; &nbsp; mConfirmPassword = findViewById(R.id.registerConfirmPassword);&nbsp; &nbsp; mRegisterButton = findViewById(R.id.registerButton);&nbsp; &nbsp; mAuth = FirebaseAuth.getInstance();&nbsp; &nbsp; mAuthListener = new FirebaseAuth.AuthStateListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (firebaseAuth.getCurrentUser() != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;startActivity(new Intent(Register.this, UploadActivity.class));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; // https://stackoverflow.com/questions/10936042/how-to-open-layout-on-button-click-android&nbsp; &nbsp; Button register = (Button) findViewById(R.id.navigate_to_login);&nbsp; &nbsp; register.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent myIntent = new Intent(view.getContext(), Login.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(myIntent, 0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; mRegisterButton.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startRegister();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}@Overrideprotected void onStart() {&nbsp; &nbsp; super.onStart();&nbsp; &nbsp; mAuth.addAuthStateListener(mAuthListener);}private void startRegister() {&nbsp; &nbsp; String email = mEmailField.getText().toString();&nbsp; &nbsp; String password = mPasswordField.getText().toString();&nbsp; &nbsp; String confirmPassword = mConfirmPassword.getText().toString();&nbsp; &nbsp; // Check that fields are not empty&nbsp; &nbsp; if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Register.this, "Email, password or confirm password field cannot be empty.", Toast.LENGTH_LONG).show();&nbsp; &nbsp; } else if (!password.equals(confirmPassword)) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; mAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onSuccess(AuthResult authResult) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Register.this, "Success", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }).addOnFailureListener(new OnFailureListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(@NonNull Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Register.this, "Failure", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}}为了确认火垒身份验证状态列表()正在启动。这是来自我的登录类的糟糕的复制和粘贴作业。这阻止了我处理成功的用户创建。然后修复程序如下所示:public class Register extends AppCompatActivity {private EditText mEmailField;private EditText mPasswordField;private EditText mConfirmPassword;private Button mRegisterButton;private FirebaseAuth mAuth;private FirebaseAuth.AuthStateListener mAuthListener;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; FirebaseApp.initializeApp(this);&nbsp; &nbsp; setContentView(R.layout.activity_register);&nbsp; &nbsp; mEmailField = findViewById(R.id.registerEmailField);&nbsp; &nbsp; mPasswordField = findViewById(R.id.registerPasswordField);&nbsp; &nbsp; mConfirmPassword = findViewById(R.id.registerConfirmPassword);&nbsp; &nbsp; mRegisterButton = findViewById(R.id.registerButton);&nbsp; &nbsp; mAuth = FirebaseAuth.getInstance();&nbsp; &nbsp; // https://stackoverflow.com/questions/10936042/how-to-open-layout-on-button-click-android&nbsp; &nbsp; Button register = (Button) findViewById(R.id.navigate_to_login);&nbsp; &nbsp; register.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent myIntent = new Intent(view.getContext(), Login.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(myIntent, 0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; mRegisterButton.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startRegister();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}@Overrideprotected void onStart() {&nbsp; &nbsp; super.onStart();}private void startRegister() {&nbsp; &nbsp; String email = mEmailField.getText().toString();&nbsp; &nbsp; String password = mPasswordField.getText().toString();&nbsp; &nbsp; String confirmPassword = mConfirmPassword.getText().toString();&nbsp; &nbsp; // Check that fields are not empty&nbsp; &nbsp; if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Register.this, "Email, password or confirm password field cannot be empty.", Toast.LENGTH_LONG).show();&nbsp; &nbsp; } else if (!password.equals(confirmPassword)) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<AuthResult> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.isSuccessful()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Register.this, "Password and confirm password should match", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java