Skip to content Skip to sidebar Skip to footer

Why Is This Problem "can't Create Handler Inside Thread" Happening On Realtime Database?

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 ha

Solution 1:

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.

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()).

Solution 2:

solved it with the help of the answer by @Frank Van Puffelen

i just needed to put

FirebaseDatabase.getInstance().getReference("xxxx").child(xxxx).addListenerForSingleValueEvent(new ValueEventListener()//problem is happening at this line
                    {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                        {
...
...
...
    }
});

inside

Handlerhandler=newHandler(Looper.getMainLooper());
                    handler.post(newRunnable() {
                        @Overridepublicvoidrun() {

.....
.....
.....}
                        });

then it worked.

Post a Comment for "Why Is This Problem "can't Create Handler Inside Thread" Happening On Realtime Database?"