Reputation: 101
I am working on a firebase app, where i have to just save content to the real-time database in firebase. i got an error in the logcat says
"Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.myfirebase. Make sure to call FirebaseApp.initializeApp(Context) first."
Even after initializing the FirebaseApp, my app is getting crash. Please help me out.
package com.example.myfirebase;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
private EditText name,email;
private Button save;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.editText3);
email = (EditText) findViewById(R.id.editText4);
save = (Button) findViewById(R.id.button);
FirebaseApp.initializeApp(this);
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AddData();
}
});
}
public void AddData(){
String Name = name.getText().toString().trim();
String Email = email.getText().toString().trim();
SaveData saveData = new SaveData(Name,Email);
databaseReference.setValue(saveData);
}
}
I want to save content to the database.
Upvotes: 2
Views: 967
Reputation: 2955
Please try:
Add FirebaseApp.initializeApp(this);
in your application class not in Activity
.
Create class extends Application
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
}
}
In Android Manifest:
<application
android:name=".MyApplication">
</application>
Upvotes: 2
Reputation: 13832
Is it possible you are working on a device without Google Play services, or didn't add com.google.gms:google-services to your project?
This page seems to indicate you get that error for Flutter apps if you leave out Google Play Services, so it could be the same for you.
Upvotes: 0
Reputation: 1974
update your project level gradle file class path of firebase :-
classpath 'com.google.gms:google-services:4.2.0'
Upvotes: 1