The Alex The Alex - 20 days ago 5
Android Question

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;
}
}

Answer

Just as qbix stated, you much use the API calls from whichever version you are going to use. If possible, you should use the newer API because it will definitely be supported much further into the future.

See the docs here:

https://firebase.google.com/docs/database/android/start/

Remove:

Firebase.setAndroidContext(this);
FirebaseApp.initializeApp(this);

And put:

FirebaseDatabase database = FirebaseDatabase.getInstance();

If qbix puts his comment into an answer, you should accept his rather than mine seeing has how he beat me by a few minutes.

Also:

If you are using the old firebase and need help switching, this guide is spot on and will help you with the switch. It's a fairly simple switch.

https://firebase.google.com/support/guides/firebase-android

Comments