1

I'm a beginner with Android Studio so I don't know much about how stuff works in it. I wrote some code for an app which uses Firebase Authentication for email/password user logging in.

This is shown in logcat as I click the 'Login' button: W/System: Ignoring header X-Firebase-Locale because its value was null.

        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email_id = emailLogin.getText().toString();
                String password_id = passwordLogin.getText().toString();

                boolean dataIsValidated = validateData(email_id, password_id);
                if (!dataIsValidated) {
                    return;
                } else {

                    loginAccount(email_id, password_id);

                }
            }

            void loginAccount(String email_id,String password_id){
                changeInProgress(true);
                FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
                firebaseAuth.signInWithEmailAndPassword(email_id,password_id).addOnCompleteListener(task -> {
                    if(task.isSuccessful()){
                        if(firebaseAuth.getCurrentUser().isEmailVerified()){
                            Toast.makeText(Actual_Login.this,"Logged in successfully!",Toast.LENGTH_SHORT).show();
                            startActivity(intentToHome);
                        }else{
                            Toast.makeText(Actual_Login.this,"This email isn't verified! Sign up with this email first",Toast.LENGTH_SHORT).show();
                            changeInProgress(false);
                        }
                    }else{
                        Toast.makeText(Actual_Login.this,"Login failed!",Toast.LENGTH_SHORT).show();
                        changeInProgress(false);
                    }
                });
            }

        }

But the app stops after showing the toast message "Logged in successfully!". The app is logged in as if I open it again, it closes again after the splash screen. Here's the full Java file which the code block above attached belongs to:

package com.brilliantbrushes.safe;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class Actual_Login extends AppCompatActivity {

    TextView movetosignup;

    EditText emailLogin,passwordLogin;
    Button loginbtn;
    ProgressBar progressLogin;
    private Intent intentToHome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_actual_login);

        Intent intentToHome = new Intent(Actual_Login.this,Home_Page.class);

        emailLogin=findViewById(R.id.emailLogin);
        passwordLogin=findViewById(R.id.passwordLogin);
        loginbtn=findViewById(R.id.loginbtn);
        progressLogin=findViewById(R.id.progressLogin);

        movetosignup = findViewById(R.id.movetosignup);
        movetosignup.setOnClickListener(view -> {
            Intent intent = new Intent(Actual_Login.this, Login_Page.class);
            startActivity(intent);
        });

        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email_id = emailLogin.getText().toString();
                String password_id = passwordLogin.getText().toString();

                boolean dataIsValidated = validateData(email_id, password_id);
                if (!dataIsValidated) {
                    return;
                } else {

                    loginAccount(email_id, password_id);

                }
            }

            void loginAccount(String email_id,String password_id){
                changeInProgress(true);
                FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
                firebaseAuth.signInWithEmailAndPassword(email_id,password_id).addOnCompleteListener(task -> {
                    if(task.isSuccessful()){
                        if(firebaseAuth.getCurrentUser().isEmailVerified()){
                            Toast.makeText(Actual_Login.this,"Logged in successfully!",Toast.LENGTH_SHORT).show();
                            startActivity(intentToHome);
                        }else{
                            Toast.makeText(Actual_Login.this,"This email isn't verified! Sign up with this email first",Toast.LENGTH_SHORT).show();
                            changeInProgress(false);
                        }
                    }else{
                        Toast.makeText(Actual_Login.this,"Login failed!",Toast.LENGTH_SHORT).show();
                        changeInProgress(false);
                    }
                });
            }

        });

    }

    void changeInProgress(boolean inProgress){
        if(inProgress){
            progressLogin.setVisibility(View.VISIBLE);
            loginbtn.setVisibility(View.GONE);
        }else{
            progressLogin.setVisibility(View.GONE);
            loginbtn.setVisibility(View.VISIBLE);
        }
    }

    boolean validateData(String email_id,String password_id){

        if (!Patterns.EMAIL_ADDRESS.matcher(email_id).matches()){
            emailLogin.setError("The email used is invalid!");
            return false;
        }
        if(password_id.length()<8){
            passwordLogin.setError("Password must be at least 8 characters long!");
            return false;
        }
        return true;
    }

};


I tried removing the finish() at the end of intentToHome. I tries cleaning and rebuilding the app. Nothing reached anywhere.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.Safe"
        tools:targetApi="31">
        <activity
            android:name=".Add_Note"
            android:exported="false" />
        <activity
            android:name=".Splash_Screen"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Actual_Login"
            android:exported="false" />
        <activity
            android:name=".Home_Page"
            android:exported="false" />
        <activity
            android:name=".Login_Page"
            android:exported="false"></activity>
    </application>

</manifest>

The Shadow
  • 11
  • 4
  • 1
    It sounds like your app crashes. When an app crashes, it writes an error message and stack trace to its logcat. Please find those, and add them to your question by clicking the `edit` link under it. Also see https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this and https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Frank van Puffelen Apr 28 '23 at 17:06
  • could it be you did not add "Home_Page" as an activity tag in the manifest file? – mindoverflow Apr 30 '23 at 01:56
  • It was working fine not so long ago and I don't remember doing anything in the manifest file. I'll try it though. – The Shadow Apr 30 '23 at 05:49
  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? Please respond using @AlexMamo – Alex Mamo May 01 '23 at 12:03
  • Besides that, if you understand Kotlin, I think that this [resource](https://medium.com/firebase-developers/how-to-authenticate-to-firebase-using-email-and-password-in-jetpack-compose-bd70ca56ea91) will help. Here is the corresponding [repo](https://github.com/alexmamo/FirebaseSignInWithEmailAndPassword). – Alex Mamo May 01 '23 at 12:05

0 Answers0