名称为[DEFAULT]的FirebaseApp不存在,出现错误 [英] FirebaseApp with name [DEFAULT] doesn't exist getting error

查看:763
本文介绍了名称为[DEFAULT]的FirebaseApp不存在,出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取有关Android后台服务的数据.但我收到此错误.这是我的代码:

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

堆栈跟踪:

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)

推荐答案

如果您的组件(最有可能出现的服务)不在应用程序的默认流程中,则必须手动初始化FirebaseApp,如下所示:

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

在访问任何其他Firebase API之前执行此操作.

Do this before accessing any other Firebase API.

每个进程都实例化它们自己的类集.例如,您的每个进程都有自己的Application对象.因此Application是一个进程内的单例 .

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通过FirebaseInitProvider类初始化,该类是ContentProvider,仅在默认进程中在应用程序启动时创建. (清单中的<provider>元素会自动合并.)

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.)

请注意,<provider>元素属性android:multiprocess="true"将不起作用,因为提供者本身没有想要与之交互的客户端.

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.

如果您想在应用程序的任何地方访问FirebaseApp,则Application类就是放置初始化代码的明显位置.

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

我说明显"不是好".我不知道这会如何影响单个Firebase服务(例如崩溃报告),或者即使在不需要Firebase的过程中,此操作也会占用大量资源.

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.

根据 AppWidgetProvider docs ,您可以创建窗口小部件UI在配置活动中或通过接收onUpdate回调.这意味着您可能需要在这两个地方都访问FirebaseApp.因此,在访问firebase之前,请同时调用示例1中的代码.

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.

这篇关于名称为[DEFAULT]的FirebaseApp不存在,出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆