FirebaseApp with name [DEFAULT] doesn't exist getting error

本秂侑毒 提交于 2019-12-12 09:55:02

问题


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)

回答1:


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.




回答2:


Instantiate objects in lifecycle callbacks.

FirebaseDatabase database;
private ValueEventListener handler;
DatabaseReference myRef;

@Override
public void onCreate() {
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("chats");
    // etc.
}



回答3:


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.




回答4:


According to the Docs, it shouldn't be required for the services I was using, but this issue resolved when I added the SHA-1 Debug Key to the online setup for the app. Click on the Settings Gear -> Project Settings. Then Add SHA1 at the bottom. See here to get Debug SHA1 Fingerprint




回答5:


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.




回答6:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!