当前位置: 首页 > 知识库问答 >
问题:

Android意图服务在firebase数据库引用上崩溃[重复]

卫弘义
2023-03-14

我正在开发一个Android应用程序,其中我使用一个意图服务,以一定的周期更新位置到我的firebase项目。但看来我相当早就碰壁了。当我在服务中给出我的firebase数据库的根引用时,它崩溃了。

import android.app.IntentService;
import android.content.Intent;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class LocationUpdater extends IntentService{

    public LocationUpdater() {
        super("LocationUpdater");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        synchronized (this){
            try{
                wait(2000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }

    }
}

什么也不做,它很好。但当我添加这一行时:

private DatabaseReference mRoot = FirebaseDatabase.getInstance().getReference();

就在LocationUpdater构造函数的上方,应用程序崩溃了。你知道为什么会这样吗?提前感谢!

这是logcat中的错误输出:

java.lang.RuntimeException: Unable to instantiate service com.mohana.cluster.LocationUpdater: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.mohana.cluster. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                                        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3168)
                                                                        at android.app.ActivityThread.-wrap5(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1563)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
                                                                     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.mohana.cluster. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                                        at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                        at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                                                                        at com.mohana.cluster.LocationUpdater.<init>(LocationUpdater.java:11)
                                                                        at java.lang.Class.newInstance(Native Method)
                                                                        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3165)
                                                                        at android.app.ActivityThread.-wrap5(ActivityThread.java) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1563) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:154) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6123) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 

共有1个答案

欧阳何平
2023-03-14

您试图获取一个Firebase实例,而不是一开始就实际初始化它。在尝试调用Firebase的实例之前,请确保您初始化了Firebase。

FirebaseApp.InitializeApp(this);

 类似资料: