0

I am developing an Android application in which I am using an intent service to update locations to my firebase project with a certain periodicity. But it seems that I have hit a wall quite early. When I give the root reference of my firebase database in the service, it crashes.

When I use this code:

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

    }
}

which does nothing, it is fine. But when I add this single line:

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

just above the LocationUpdater constructor, the app crashes. Any ideas on why this is happening? Thanks in advance!

This is the error output in 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) 
The doctor
  • 29
  • 1
  • 8

1 Answers1

1

You are trying to get an instance of Firebase without actually initializing it in the first place. Make sure you initialize Firebase before trying to call an instance of it.

FirebaseApp.initializeApp(this);