如何修复“应用程序崩溃并在mAuth = FirebaseAuth.getInstance()上给出错误”

时间:2019-04-13 10:34:45

标签: java android firebase firebase-authentication

我的应用程序崩溃了并且给出了错误 'mAuth = FirebaseAuth.getInstance();'中的onCreate

我尝试了不同版本的依赖关系,但面临相同的问题。

public class MainActivity extends AppCompatActivity{

    private FirebaseAuth mAuth;
    private Button buttonLogin;
    private EditText editTextEmail;
    private EditText editTextPass;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Initialize Firebase Auth
'''        mAuth = FirebaseAuth.getInstance();

        editTextEmail = findViewById(R.id.editText_username);
        editTextPass = findViewById(R.id.editText2_password);

        buttonLogin = findViewById(R.id.buttonLogin);

        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String email = editTextEmail.getText().toString().trim();
                String password = editTextPass.getText().toString().trim();

                mAuth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    Toast.makeText(MainActivity.this, "Login Successfully", Toast.LENGTH_LONG).show();
                                } else {
                                    Toast.makeText(MainActivity.this, "User not exist", Toast.LENGTH_LONG).show();
                                }

                                // ...
                            }
                        });
            }
        });
    }
}
```[here is log error][1]
[dependencies][2]


  [1]: https://i.stack.imgur.com/8Edqa.png
  [2]: https://i.stack.imgur.com/alXe0.png

1 个答案:

答案 0 :(得分:0)

尝试这种方式:-

如果您的错误显示“初始化Firebase身份验证”,然后更新您的项目级gradle文件并更新google classpath:-

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



mAuth = FirebaseAuth.getInstance();

if(mAuth.getCurrentUser() != null){
// user exist
}else{
 //user not exist
}