Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase使用EmailAndPassword登录,CreateUserwith EmailAndPassword在Android中不工作_Android_Firebase_Firebase Authentication_Firebase Storage - Fatal编程技术网

Firebase使用EmailAndPassword登录,CreateUserwith EmailAndPassword在Android中不工作

Firebase使用EmailAndPassword登录,CreateUserwith EmailAndPassword在Android中不工作,android,firebase,firebase-authentication,firebase-storage,Android,Firebase,Firebase Authentication,Firebase Storage,我正在开发一个Android应用程序,在这个应用程序中,我通过电子邮件和密码使用Firebase身份验证进行用户登录。我使用了Firebase提供的基本教程,但在输入用户ID和密码后,我发现“登录”或“创建帐户”按钮都不起作用 当我调试它时,我发现控件不会: public void onComplete(@NonNull Task<AuthResult> task) public void onComplete(@NonNull任务) 在: mAuth.signwithEmail

我正在开发一个Android应用程序,在这个应用程序中,我通过电子邮件和密码使用Firebase身份验证进行用户登录。我使用了Firebase提供的基本教程,但在输入用户ID和密码后,我发现“登录”或“创建帐户”按钮都不起作用

当我调试它时,我发现控件不会:

public void onComplete(@NonNull Task<AuthResult> task)
public void onComplete(@NonNull任务)
在:

mAuth.signwithEmailandPassword(电子邮件,密码)。addOnCompleteListener(这是新的OnCompleteListener()
或:

mAuth.createUserWithEmailAndPassword(电子邮件,密码).addOnCompleteListener(这是新的OnCompleteListener()
登录和创建帐户的代码为:

private void createAccount(String email, String password)
{
    Log.d(TAG, "createAccount:" + email);
    if (!validateForm())
    {
        return;
    }

    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
    {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task)
                {


                        if (task.isSuccessful()) 
                       {
                            Log.d(TAG, "createUserWithEmail:success");
                            FirebaseUser user = fAuth.getCurrentUser();
                            updateUI(user);
                        } 

                       else 
                       {
                         Log.w(TAG,"createUserWithEmail:failure",task.getException());
                            Toast.makeText(LoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
                            updateUI(null);
                        }

                }
    });
}

private void signIn(String email, String password)
{
    Log.d(TAG, "signIn:" + email);
    if (!validateForm())
    {
        return;
    }

    mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
    {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task)
        {
            if (task.isSuccessful())
            {
                Log.d(TAG, "signInWithEmail:success");
                FirebaseUser user = fAuth.getCurrentUser();
                updateUI(user);
            }

            else
            {
                Log.w(TAG, "signInWithEmail:failure", task.getException());
                Toast.makeText(LoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
                updateUI(null);
            }
            if (!task.isSuccessful())
            {
                mStatusTextView.setText(R.string.auth_failed);
            }

        }
    });

}
private void createAccount(字符串电子邮件、字符串密码)
{
Log.d(标记“createAccount:”+电子邮件);
如果(!validateForm())
{
返回;
}
mAuth.createUserWithEmailAndPassword(电子邮件,密码).addOnCompleteListener(这是新的OnCompleteListener())
{
@凌驾
未完成的公共void(@NonNull任务)
{
if(task.issusccessful())
{
Log.d(标记“createUserWithEmail:success”);
FirebaseUser=fAuth.getCurrentUser();
updateUI(用户);
} 
其他的
{
w(标记“createUserWithEmail:failure”,task.getException());
Toast.makeText(LoginActivity.this,“身份验证失败”,Toast.LENGTH_SHORT.show();
updateUI(null);
}
}
});
}
私有无效登录(字符串电子邮件、字符串密码)
{
Log.d(标记“签名:”+电子邮件);
如果(!validateForm())
{
返回;
}
mAuth.signInWithEmailAndPassword(电子邮件,密码).addOnCompleteListener(这是新的OnCompleteListener())
{
@凌驾
未完成的公共void(@NonNull任务)
{
if(task.issusccessful())
{
Log.d(标记“signInWithEmail:success”);
FirebaseUser=fAuth.getCurrentUser();
updateUI(用户);
}
其他的
{
w(标记“signInWithEmail:failure”,task.getException());
Toast.makeText(LoginActivity.this,“身份验证失败”,Toast.LENGTH_SHORT.show();
updateUI(null);
}
如果(!task.issusccessful())
{
mStatusTextView.setText(R.string.auth_失败);
}
}
});
}

它只是跳过了两个方法中的内部部分,没有返回任何内容。我按照与教程中完全相同的方式进行操作,但我不明白确切的问题是什么。请有人帮助我这里的错误所在。

对代码进行以下更改

声明AuthListener:

       // [START declare_auth_listener]
    private FirebaseAuth.AuthStateListener mAuthListener;
    // [END declare_auth_listener]

   // [START declare_auth]
    private FirebaseAuth mAuth;
    // [END declare_auth]
在onStart()和onStop()中添加listner:

然后在活动的onCreate()中添加AuthStateListner方法,它将在每次创建活动时检查身份验证状态:

// [START auth_state_listener] ,this method execute as soon as there is a change in Auth status , such as user sign in or sign out.
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();

                if (user != null) {   
                    // User is signed in        
                    //redirect 
                  updateUI(user);

                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                  updateUI(null);
                }

            }
        };
 // [END auth_state_listener]
要使用电子邮件密码登录,请执行以下操作

        private void signIn(String email, String password)
       {
    Log.d(TAG, "signIn:" + email);
    if (!validateForm())
    {
        return;
    }

    mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
    {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task)
        {
         Log.d(LOG_TAG, " Verification : signIn With Email:onComplete:" + task.isSuccessful());
                    //  If sign in succeeds i.e if task.isSuccessful(); returns true then the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.


           // If sign in fails, display a message to the user.
             if (!task.isSuccessful()) {
                        try {
                            throw task.getException();
                        } catch (FirebaseAuthInvalidUserException e) {
                            mStatusTextView.setError("Invalid Emaild Id");
                            mStatusTextView.requestFocus();
                        } catch (FirebaseAuthInvalidCredentialsException e) {
                            Log.d(LOG_TAG , "email :" + email);
                            mStatusTextView.setError("Invalid Password");
                            mStatusTextView.requestFocus();
                        } catch (FirebaseNetworkException e) {
                            showErrorToast("error_message_failed_sign_in_no_network");
                        } catch (Exception e) {
                            Log.e(LOG_TAG, e.getMessage());
                        }
                        Log.w(LOG_TAG, "signInWithEmail:failed", task.getException());
                        Toast.makeText(LoginActivity.this, R.string.login_error,
                                Toast.LENGTH_SHORT).show();
                        updateUI(null);
        }
    });

}
private void登录(字符串电子邮件、字符串密码)
{
Log.d(标记“签名:”+电子邮件);
如果(!validateForm())
{
返回;
}
mAuth.signInWithEmailAndPassword(电子邮件,密码).addOnCompleteListener(这是新的OnCompleteListener())
{
@凌驾
未完成的公共void(@NonNull任务)
{
Log.d(Log_标签,“验证:通过电子邮件登录:onComplete:”+task.issusccessful());
//如果登录成功,即如果task.issusccessful();返回true,则将通知身份验证状态侦听器并使用逻辑来处理
//可以在侦听器中处理已登录用户。
//如果登录失败,则向用户显示消息。
如果(!task.issusccessful()){
试一试{
抛出task.getException();
}捕获(FirebaseAuthInvalidUserException e){
mStatusTextView.setError(“无效的电子邮件Id”);
mStatusTextView.requestFocus();
}捕获(FirebaseAuthInvalidCredentialsException){
Log.d(Log_标签,“email:+email”);
mStatusTextView.setError(“无效密码”);
mStatusTextView.requestFocus();
}捕获(FirebaseNetworkException e){
阵雨警告(“错误消息失败,在网络中签名”);
}捕获(例外e){
Log.e(Log_标记,e.getMessage());
}
Log.w(Log_标记,“signInWithEmail:failed”,task.getException());
Toast.makeText(LoginActivity.this,R.string.login\u错误,
吐司。长度(短)。show();
updateUI(null);
}
});
}

您是否在控制台中启用了firebase登录并使用电子邮件和密码?您是否向AndroidManifest添加了internet权限?@MartinDeSimone:是。我在控制台中启用了它。@PatrixWilliams:是。我在清单文件中添加了internet权限。我在日志“未找到com.google.firebase.auth的本地模块描述符类”中找到了它。wh控件不在create方法中。这是原因吗?这非常有用,谢谢。我可以问一下您从哪里得到可能的异常列表吗?谢谢您的欣赏。我是从firebase文档中得到的
@Override
public void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}
// [START auth_state_listener] ,this method execute as soon as there is a change in Auth status , such as user sign in or sign out.
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();

                if (user != null) {   
                    // User is signed in        
                    //redirect 
                  updateUI(user);

                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                  updateUI(null);
                }

            }
        };
 // [END auth_state_listener]
        private void signIn(String email, String password)
       {
    Log.d(TAG, "signIn:" + email);
    if (!validateForm())
    {
        return;
    }

    mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
    {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task)
        {
         Log.d(LOG_TAG, " Verification : signIn With Email:onComplete:" + task.isSuccessful());
                    //  If sign in succeeds i.e if task.isSuccessful(); returns true then the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.


           // If sign in fails, display a message to the user.
             if (!task.isSuccessful()) {
                        try {
                            throw task.getException();
                        } catch (FirebaseAuthInvalidUserException e) {
                            mStatusTextView.setError("Invalid Emaild Id");
                            mStatusTextView.requestFocus();
                        } catch (FirebaseAuthInvalidCredentialsException e) {
                            Log.d(LOG_TAG , "email :" + email);
                            mStatusTextView.setError("Invalid Password");
                            mStatusTextView.requestFocus();
                        } catch (FirebaseNetworkException e) {
                            showErrorToast("error_message_failed_sign_in_no_network");
                        } catch (Exception e) {
                            Log.e(LOG_TAG, e.getMessage());
                        }
                        Log.w(LOG_TAG, "signInWithEmail:failed", task.getException());
                        Toast.makeText(LoginActivity.this, R.string.login_error,
                                Toast.LENGTH_SHORT).show();
                        updateUI(null);
        }
    });

}