Firebase:createUserWithEmailAndPassword中的onComplete()永远不会运行,,

时间:2017-11-01 09:42:50

标签: android firebase firebase-authentication

好日子 我正在尝试使用firebase注册一个新帐户,但onComplete方法它永远不会运行我试图检查是否有任何错误 by break point 但是它从未到达过它..我试过确保密码不低于6个字符,并且在firebase控制台中启用了电子邮件/密码验证。 这是我的代码

        public class CreateAccountActivity extends AppCompatActivity {

    private EditText emailedittxt;
    private EditText passwordedittext;
    private Button signUp;
    //private EditText nametext;
    private static final String TAG = "EmailPassword";
    //Authentication
    private FirebaseAuth mAuth;

    //    public static Intent newIntent(Context packageContext){
//        Intent intent = new Intent(packageContext,CreateAccountActivity.class);
//        return intent;
//    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_account);

        mAuth = FirebaseAuth.getInstance();

//        nametext = (EditText) findViewById(R.id.RnameText);
        emailedittxt = (EditText) findViewById(R.id.RemailText);
        passwordedittext = (EditText) findViewById(R.id.RpasswordText);
        signUp = (Button) findViewById(R.id.RsignupButton);
        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // signUp();
                //create new Account
//                String name = nametext.getText().toString();
                String email = emailedittxt.getText().toString();
                String password = passwordedittext.getText().toString();
                //validate the felids

//                if (TextUtils.isEmpty(name)) {
//                    Toast.makeText(CreateAccountActivity.this, "Enter your nmae", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                if (TextUtils.isEmpty(email)) {
                    Toast.makeText(CreateAccountActivity.this, "Enter your email", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(CreateAccountActivity.this, "Enter your password", Toast.LENGTH_SHORT).show();
                    return;
                }

                /*Create
                new account */

                SignUp(email,password);

            }
        });
    }
    private void SignUp(String email,String password) {
        mAuth.createUserWithEmailAndPassword(email, password)


                .addOnCompleteListener(CreateAccountActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
//                       // Toast.makeText(CreateAccountActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
//                        // signed in user can be handled in the listener.
                        if (task.isSuccessful()) {

                            Intent intent = new Intent (CreateAccountActivity.this,Main2Activity.class);
                            startActivity(intent);
                            //  startActivity(new Intent(CreateAccountActivity.this, Main2Activity.class));
                            finish();

                        } else {
                            Log.d(TAG, "onComplete: Failed=" + task.getException().getMessage()); //ADD THIS
//
////                            Toast.makeText(CreateAccountActivity.this, "Authentication failed." + task.getException(), Toast.LENGTH_SHORT).show();

                        }}
                });
    }
}

任何人都知道为什么它永远不会运行onComplete()

1 个答案:

答案 0 :(得分:0)

尝试添加onFailureListener()

 private void SignUp(String email,String password) {
    mAuth.createUserWithEmailAndPassword(email, password)


            .addOnCompleteListener(CreateAccountActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());
  //                       // Toast.makeText(CreateAccountActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
//                        // signed in user can be handled in the listener.
                    if (task.isSuccessful()) {

                        Intent intent = new Intent (CreateAccountActivity.this,Main2Activity.class);
                        startActivity(intent);
                        //  startActivity(new Intent(CreateAccountActivity.this, Main2Activity.class));
                        finish();

                    } else {
                        Log.d(TAG, "onComplete: Failed=" + task.getException().getMessage()); //ADD THIS
//
////                            Toast.makeText(CreateAccountActivity.this, "Authentication failed." + task.getException(), Toast.LENGTH_SHORT).show();

                    }}
            }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e("exception",e.getMessage());
            }
        });
}