方法onComplete不起作用

时间:2018-05-21 07:28:39

标签: java android firebase firebase-authentication


我试图为Android制作一个应用程序。
应用程序必须具有登录和注册表单,我无法理解为什么方法onComplete不起作用,但数据被添加到注册页面的数据库中。
这是代码:

private void submitForm(String email, String password){
    auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    FirebaseAuthException e = (FirebaseAuthException)task.getException();
                    if(task.isSuccessful()){
                        Toast.makeText(RegisterActivity.this, "Registration successfully!", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                        finish();
                    }else{
                        Toast.makeText(RegisterActivity.this, "" +e.getMessage(),
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });
}

private void validate(String email, String password, String confirmPassword){
    if(!TextUtils.isEmpty(email)) {
        if (android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            if ((!TextUtils.isEmpty(password)) && (!TextUtils.isEmpty(confirmPassword))) {
                if (!password.equals(confirmpassword)) {
                    if(password.length() > 6){
                        submitForm(email, password);
                    }else{
                        Toast.makeText(this, "Password must be at least 6 characters.", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "Passwords do not match.", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "Please fill in both password fields.", Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(this, "Please enter a valid email address.", Toast.LENGTH_SHORT).show();
        }
    }else {
        Toast.makeText(this, "Please fill the email field.", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    auth = FirebaseAuth.getInstance();
    email = findViewById(R.id.emailEditText);
    password = findViewById(R.id.passwordEditText);
    confirmpassword = findViewById(R.id.confirmpasswordEditText);
    register = findViewById(R.id.registerbutton);

    register.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            validate(email.getText().toString(), password.getText().toString(), confirmpassword.getText().toString());
        }
    });
}

当我向数据库添加数据时,我希望在登录页面上播放。

1 个答案:

答案 0 :(得分:1)

将Firebase更新为最新版本时,请确保同时更新您的Google Play服务

在我使用Firebase 15+的情况下,我必须设置google play服务(在项目gradle中)

classpath 'com.google.gms:google-services:3.3.1'

还可以尝试清除数据&amp; 重新安装应用选项。