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)
Instantiate objects in lifecycle callbacks.
FirebaseDatabase database;
private ValueEventListener handler;
DatabaseReference myRef;
@Override
public void onCreate() {
database = FirebaseDatabase.getInstance();
myRef = database.getReference("chats");
// etc.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With