Firebase createUserWithEmailAndPassword成功,但oncomplete永远不会运行

时间:2017-01-31 07:26:33

标签: android firebase firebase-authentication

尝试使用firebase注册新帐户时,帐户已成功创建,但OnCompleteListener从未触发,甚至不会触发OnFailureListener。

我正在尝试这样做,以便在尝试登录帐户时,系统将首先检查帐户是否存在,如果它确实尝试登录,如果它没有创建新账户。当我只使用createUserWithEmailAndPassword时,会创建一个新帐户但我的应用程序立即关闭,并且" OnCompleteListener"或" OnFailureListener"永远不会跑。

我将能够在这些代码之前和logcat中的这些代码之后看到日志,但这些代码中的日志都没有记录在logcat中。

auth.signInWithEmailAndPassword(mEmail, mPassword)

                    .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>()

                    {

                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Log.w(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());

                            // 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()) {
                                auth.createUserWithEmailAndPassword(mEmail, mPassword)
                                        .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                                            @Override
                                            public void onComplete(@NonNull Task<AuthResult> task) {
                                                Log.d(TAG, "Start Testing"+task.getException() );

                                                // 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()) {
                                                    Log.d(TAG, "Testing Not Success" + task.getException());
                                                    task.getException();

                                                    Log.d(TAG, "Testing Not Success 2" + task.isSuccessful());

                                                }else{
                                                    Log.d(TAG, "Testing is Success" + task.isSuccessful());
                                                    // startActivity(new Intent(LoginActivity.this, MainMenuActivity.class));
                                                    // finish();
                                                }

                                                // ...
                                            }
                                        });
                            }else{
                                Log.w(TAG, "signInCompleted");
                                Toast.makeText(LoginActivity.this, "HI bitch",
                                        Toast.LENGTH_SHORT).show();
                                LoginActivity.this.startActivity(new Intent(LoginActivity.this, MainMenuActivity.class));
                                //finish();
                            }

                            // ...
                        }
                    })
                    .addOnFailureListener(LoginActivity.this, new OnFailureListener() {

                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d(TAG, "Start Testing"+e );


                }
            });

1 个答案:

答案 0 :(得分:1)

我刚刚使用onSuccessListener而不是onCompleteListener修复了这个问题。我假设你在这一点上放弃或解决了这个问题,但我无法在任何地方找到答案,所以代码就是这样的

auth.signInWithEmailAndPassword(mEmail, mPassword)
          .addOnSuccessListener(LoginActivity.this, new OnSuccessListener<AuthResult>(){

                    @Override
                    public void onsuccess(@NonNull AuthResult authResult){
                         //your code goes here
                    }
                 });