Hi I am trying to get data on background service of Android. But I am getting this error. Here is my code:
public class FirebaseBackgroundService extends Service {
FirebaseDatabase database = FirebaseDatabase.getInstance();
private ValueEventListener handler;
DatabaseReference myRef = database.getReference("chats");
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
handler = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
postNotify(dataSnapshot.getValue().toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
myRef.addValueEventListener(handler);
}
}
Stack trace:
java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2862)
at android.app.ActivityThread.-wrap4(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.
at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
at bagga2.example.com.liiv.services.FirebaseBackgroundService.<init>(FirebaseBackgroundService.java:30)
at java.lang.Class.newInstance(Native Method)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2859)
at android.app.ActivityThread.-wrap4(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
If your component (service most likely) does not live in the app's default process you have to manually initialize FirebaseApp like so:
FirebaseApp.initializeApp(context, FirebaseOptions.fromResource(context));
Do this before accessing any other Firebase API.
Implementation Background
Every process instantiates their own set of classes. For example each of your processes has its own Application object. Therefore Application is a singleton within one process.
FirebaseApp in default process is initialized via FirebaseInitProvider class which is a ContentProvider that is created at application start only in default process. (The <provider> element in manifest is merged in automatically.)
Please note that the <provider> element attribute android:multiprocess="true" would have no effect as the provider itself has no clients who'd like to interact with it.
Example #1: Init Everywhere
If you want access to a FirebaseApp everywhere in your app an obvious place to put init code is your Application class.
try {
FirebaseApp.getInstance()
} catch (IllegalStateException ex) {
FirebaseApp.initializeApp(context, FirebaseOptions.fromResource(context));
}
I said "obvious" not "good". I have no idea how this impacts individual Firebase services (such as crash reporting) or how resource intense this operation is to do even in processes where you don't need Firebase.
Example #2: App Widgets (Init Only When Needed)
According to AppWidgetProvider docs you either create widget UI in a configuration activity or by receiving an onUpdate callback. Which means you probably need access to FirebaseApp in both those places. So call the code from Example #1 in both of them before accessing firebase.
Instantiate objects in lifecycle callbacks.
FirebaseDatabase database;
private ValueEventListener handler;
DatabaseReference myRef;
@Override
public void onCreate() {
database = FirebaseDatabase.getInstance();
myRef = database.getReference("chats");
// etc.
}
Be sure you have installed Firebase SDK correctly and that you have added the line:
apply plugin: 'com.google.gms.google-services'
at the bottom of your app level build.gradle file. Then rebuild the project, that solved the problem in my case.
I might be late but you have to add this in your app gradle
apply plugin: 'com.google.gms.google-services'
and
classpath 'com.google.gms:google-services:3.0.0'
at your project level gradle.
please check if google-services.json file is added in your project or not , if not add it and the rebuild project..
来源:https://stackoverflow.com/questions/37531749/firebaseapp-with-name-default-doesnt-exist-getting-error
