createUserWithEmailAndPassword无效

时间:2016-12-29 12:27:47

标签: android firebase firebase-authentication

一切看起来都不错,我只想在firebase注册一个用户,但它不起作用。这是我的活动:

public class SignUp extends Activity {
    Button signup1;
    EditText email1, password1, password2;
    FirebaseAuth firebaseAuth;
    ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        //Instantiate the FirebaseAuth object
        firebaseAuth = FirebaseAuth.getInstance();

        signup1 = (Button) findViewById(R.id.signup);
        // Setting the listener for button
        signup1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
        email1 = (EditText) findViewById(R.id.email);
        password1 = (EditText) findViewById(R.id.password1);
        password2 = (EditText) findViewById(R.id.password2);
        progressDialog = new ProgressDialog(this);
    }

    public void register() {
        String email = email1.getText().toString().trim();
        String pass1 = password1.getText().toString().trim();
        String pass2 = password2.getText().toString().trim();

                 if (TextUtils.isEmpty(email)) {
                     Toast.makeText(this, "Please Enter an Email", Toast.LENGTH_SHORT).show();
                     return;
                 }
                 if (TextUtils.isEmpty(pass1)) {
                     Toast.makeText(this, "Please Enter Password", Toast.LENGTH_SHORT).show();
                     return;
                 }
                 if (TextUtils.isEmpty(pass2)) {
                     Toast.makeText(this, "Please Enter Password Again", Toast.LENGTH_SHORT).show();
                     return;
                 }
                 if (TextUtils.equals(pass1, pass2)) {
                 progressDialog.setMessage("Registering....!");
                     progressDialog.show();
                firebaseAuth.createUserWithEmailAndPassword(email, pass2).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Toast.makeText(SignUp.this,"SignUp Successfully", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(SignUp.this, "SignUp Failed...  Try Again", Toast.LENGTH_SHORT).show();
                    }
                    progressDialog.dismiss();
                }
            });

             }
             else {
                     Toast.makeText(this, "Password does not Match", Toast.LENGTH_SHORT).show();
                     return;
                 }
        }
 }

当应用程序进入&#34; createUserWithEmailAndPassword&#34;时,应用程序停滞不前。

1 个答案:

答案 0 :(得分:0)

使用try-catch进行异常处理。 例如,createUser ...方法为null并捕获并解析。