Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseApp not initializing despite FirebaseApp.initializeApp() being called in Application class

I am creating an Android application and I'm currently trying to implement user authentication using Firebase. As far as I can tell, my app is connected to my Firebase server.

I encounter a runtime error when attempting to switch from the SignIn activity to the SignUp activity via a button press. The app crashes and I encounter a runtime error.

So far as I can tell, the runtime error is from the SignUp activity's onCreate() call when I attempt to initialize a FirebaseAuth object with FirebaseAuth.getInstance(). This call fails due to

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process seniordesign.phoneafriend. Make sure to call FirebaseApp.initializeApp(Context).

However, I make this call in my Application class' onCreate() method which I thought would be fine. I added the initalizeApp() call to the SignUp's onCreate() call but no dice. I've looked for others with this issue but have not found anything similar. Thanks for any help.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="seniordesign.phoneafriend">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name="seniordesign.phoneafriend.PhoneAFriend">
        <activity android:name=".SignIn">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SignUp"></activity>
    </application>

</manifest>

PhoneAFriend.java (My Application class)

public class PhoneAFriend extends Application {

    public void onCreate(){
        super.onCreate();
        Firebase.setAndroidContext(this);
        FirebaseApp.initializeApp(this);
    }
}

SignUp.java

public class SignUp extends AppCompatActivity {
    protected Firebase ref;
    protected EditText emailText;
    protected EditText passText;
    protected EditText confirmText;
    protected Button button;
    protected SignUp thisContext;

    protected FirebaseAuth auth;
    protected FirebaseAuth.AuthStateListener authListener;
    private View.OnClickListener onClickListener;

    public static Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        ref = new Firebase("https://phoneafriend-7fb6b.firebaseio.com");
        emailText = (EditText) findViewById(R.id.signup_emailText);
        passText = (EditText) findViewById(R.id.signup_passwordText);
        confirmText = (EditText) findViewById(R.id.signup_passwordConfirm);
        intent = new Intent(this, SignIn.class);
        //Tried this already
        //FirebaseApp.initializeApp(this);
        auth = FirebaseAuth.getInstance();


        button = (Button) findViewById(R.id.signup_button);
        onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createUser(view);
                Log.v("SignUp Button" , "Clicked; Attempting to create user");
            }
        };
        button.setOnClickListener(onClickListener);

        authListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged( FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d("FirebaseAuth", "onAuthStateChanged:signed_in:" + user.getUid());
                } else {
                    // User is signed out
                    Log.d("FirebaseAuth", "onAuthStateChanged:signed_out");
                }
                // ...
            }
        };
        thisContext = this;

    }
    @Override
    public void onStart(){
        super.onStart();
        //auth.addAuthStateListener(authListener);
    }

    @Override
    public void onStop(){
        super.onStop();
        if(authListener != null) {
            //auth.removeAuthStateListener(authListener);
        }
    }

    protected void createUser(View view){
        String cString = null;
        String pString = null;
        String eString  = emailText.getText().toString();
        if(passText.getText() != null && confirmText.getText() != null) {
            pString = passText.getText().toString();
            cString = confirmText.getText().toString();
            Log.v("SignUP: Pass Null check" , "Pass" );
            if (emailText.getText() != null && pString.equals(cString) && passText.getText() != null) {
                Log.v("SignUP: Sign up check " , "Pass");
                auth.createUserWithEmailAndPassword(emailText.getText().toString() , passText.getText().toString())
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                Log.v("createUser complete" , "status: " + task.isSuccessful());
                                if(task.isSuccessful()){
                                    startActivity(SignUp.intent);
                                }
                            }

                        });

            }
        }

        return;
    }
}
like image 289
The Alex Avatar asked Jan 17 '26 09:01

The Alex


1 Answers

I know there's already an accepted answer. However, I ran into the same error message saying: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process (name). Make sure to call FirebaseApp.initializeApp(Context). I tried several solutions found on SO, and double checking everything, until I finally found that the package name defined in the Firebase Console didn't match the package name defined in my manifest file.

Try go to your Firebase Console -> Project settings -> check if package names matches.

Hope it may help some :)

like image 92
FJJ Avatar answered Jan 20 '26 02:01

FJJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!