Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 573
Davin Avatar asked Dec 25 '22 05:12

Davin


1 Answers

Instantiate objects in lifecycle callbacks.

FirebaseDatabase database;
private ValueEventListener handler;
DatabaseReference myRef;

@Override
public void onCreate() {
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("chats");
    // etc.
}
like image 94
Eugen Pechanec Avatar answered Dec 28 '22 10:12

Eugen Pechanec