简体   繁体   中英

how to fix 'application crashing and giving error on mAuth = FirebaseAuth.getInstance();'

My application is crashing and giving error 'mAuth = FirebaseAuth.getInstance();' in onCreate method.

I tried different version of dependencies but facing same problem.

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

try this way :-

if your error is showing Initialize Firebase Auth then update your project level gradle file and update the google classpath:-

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



mAuth = FirebaseAuth.getInstance();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM