I am building a simple firebase login and signup application. It contains three activities, SignUp, LogIn and Profile activities. I have integrated Firebase, but the app crashes with an error when I click on either LogIn or SignUp button. I have gone through the entire forum looking for possible fix but couldn't find a right solution to fix my problem. How can I fix this problem?
Sharing my Login activity as reference code:-
public class LoginActivity extends AppCompatActivity {
//view binding
private ActivityLoginBinding binding;
//firebase auth
private FirebaseAuth firebaseAuth;
//progress dialog
private ProgressDialog progressDialog;
//actionBar
private ActionBar actionBar;
private String email="", password="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityLoginBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
//Configure action bar, title
actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle("LogIn");
}
//Configure progress dialog
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Please wait");
progressDialog.setMessage("Logging In");
progressDialog.setCanceledOnTouchOutside(false);
//Initialise firebase auth
firebaseAuth = FirebaseAuth.getInstance();
checkUser();
//if have account, SignUp
binding.haveAccountTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this ,SignUpActivity.class));
finish();
}
});
binding.loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validateData();
}
});
}
private void checkUser() {
//check if user is already logged in
//get current user
//if already logged in, then open profile activity
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if(firebaseUser != null){
//user is already logged in
startActivity(new Intent(this, ProfileActivity.class));
finish();
}
}
//Method 1 : Validate User Data
private void validateData() {
String email = binding.emailEt.getText().toString().trim();
String password = binding.passwordEt.getText().toString().trim();
if(TextUtils.isEmpty(email)){
binding.emailEt.setError("This field cannot be empty");
}
else if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
binding.emailEt.setError("Invalid Email format");
}
else if(TextUtils.isEmpty(password)){
binding.passwordEt.setError("This field cannot be empty");
}
else if(password.length() < 6){
binding.passwordEt.setError("Password must be atleast 6 characters long");
}
else {
//data is valid. Now continue to firebase LogIn.
firebaseLogIn();
}
}
//Method 2 : Firebase Log in
private void firebaseLogIn(){
//show progress
progressDialog.show();
firebaseAuth.signInWithEmailAndPassword(email,password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
//login success
progressDialog.dismiss();
//get user info
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String email = null;
if (firebaseUser != null) {
email = firebaseUser.getEmail();
}
Toast.makeText(LoginActivity.this, "LoggedIn\n"+email, Toast.LENGTH_SHORT).show();
//open profile activity
startActivity(new Intent(LoginActivity.this, ProfileActivity.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//login failed. get and show error message
progressDialog.dismiss();
Toast.makeText(LoginActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
Also sharing the logcat below:-
--------- beginning of crash
2022-01-27 09:48:19.063 4915-4915/com.jaswikventures.firebaseloginsignup E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jaswikventures.firebaseloginsignup, PID: 4915
java.lang.IllegalArgumentException: Given String is empty or null
at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(com.google.android.gms:play-services-basement@@18.0.0:2)
at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(com.google.firebase:firebase-auth@@21.0.1:1)
at com.jaswikventures.firebaseloginsignup.LoginActivity.firebaseLogIn(LoginActivity.java:118)
at com.jaswikventures.firebaseloginsignup.LoginActivity.validateData(LoginActivity.java:109)
at com.jaswikventures.firebaseloginsignup.LoginActivity.access$000(LoginActivity.java:22)
at com.jaswikventures.firebaseloginsignup.LoginActivity$2.onClick(LoginActivity.java:72)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2022-01-27 09:48:19.065 530-3813/? W/ActivityTaskManager: Force finishing activity com.jaswikventures.firebaseloginsignup/.LoginActivity
I thought covering below code with if statement would solve the problem but it didn't work.
String email = null;
if (firebaseUser != null) {
email = firebaseUser.getEmail();
}
You have declared these as being local in validateData
String email = binding.emailEt.getText().toString().trim();
String password = binding.passwordEt.getText().toString().trim();
When you call
firebaseAuth.signInWithEmailAndPassword(email,password)
they are empty
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments