为什么这个问题“无法在线程内创建处理程序"?实时数据库上发生的事情? [英] why is this problem "Can't create handler inside thread" happening on realtime database?
问题描述
所以我试图从firebasemessagingservice内部调用实时数据库以从数据库中获取一些信息,但是每次应用程序在后台运行时都会发生此错误
so i'm trying to call realtime database from inside a firebasemessagingservice to get some infos from the database but each and every time the app is on background this error is happening
09-17 19:27:21.596 4083-4140/? E/AndroidRuntime: FATAL EXCEPTION: Firebase-MyFirebaseMessagingService
Process: com.kirtu.simpletexts.texts, PID: 4083
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at com.google.firebase.database.zza.<init>(com.google.firebase:firebase-database@@16.0.2:1024)
at com.google.firebase.database.obfuscated.zzb.zza(com.google.firebase:firebase-database@@16.0.2:77)
at com.google.firebase.database.obfuscated.zzu.zza(com.google.firebase:firebase-database@@16.0.2:2234)
at com.google.firebase.database.obfuscated.zzad.zzb(com.google.firebase:firebase-database@@16.0.2:92)
at com.google.firebase.database.obfuscated.zzad.zza(com.google.firebase:firebase-database@@16.0.2:42)
at com.google.firebase.database.FirebaseDatabase.zza(com.google.firebase:firebase-database@@16.0.2:357)
at com.google.firebase.database.FirebaseDatabase.getReference(com.google.firebase:firebase-database@@16.0.2:201)
at com.kirtu.simpletexts.texts.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:60)
at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source)
at com.google.firebase.iid.zzc.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source)
at java.lang.Thread.run(Thread.java:761)
为什么这个问题突然发生?以前从未发生过.
why is this problem happening suddenly?previously it never happened.
这是我的代码
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
...
...
...
FirebaseDatabase.getInstance().getReference("xxxx").child(xxxx).addListenerForSingleValueEvent(new ValueEventListener()//problem is happening at this line
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
...
...
...
}
});
提前谢谢.
推荐答案
应该从主线程调用Firebase Realtime Database客户端.更具体地说,Firebase客户端在应用程序的主/UI线程上调用onDataChange
回调.由于onMessageReceived
是服务的一部分,因此可以在主/UI线程不活动时调用它.而且,虽然我以前从未见过,但收到的错误消息是发生这种情况时的结果.
Calls to the Firebase Realtime Database client are expected to be made from the main thread. More specifically, the Firebase client invokes the onDataChange
callback on the main/UI thread of your app. Since onMessageReceived
is part of a service, it may be called when the main/UI thread is not active. And, while I've never seen it before, the error message you get is the result when that happens.
技巧是将onMessagesReceived
从发布到主线程(请参阅 Firebase Cloud Messaging:在onMessageReceived()中访问UI元素.
The trick is to post from your onMessagesReceived
to the main thread (see Access data from Firebase Service in main thread), or ensure that the looper is initialized so that your code can be called from the correct context (see Firebase Cloud Messaging: Accessing UI elements in onMessageReceived()).
这篇关于为什么这个问题“无法在线程内创建处理程序"?实时数据库上发生的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!