Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix null sending message to a Handler on a dead thread warning?

Tags:

android

I have a thread using handler and messages to send data to the activity. Everything work fine except when the activity is paused :

null sending message to a Handler on a dead thread
java.lang.RuntimeException: null sending message to a Handler on a dead thread
    at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
    at android.os.Looper.quit(Looper.java:173)
    at pocket.net.ComD.stopConnection(ComD.java:154)
    at pocket.net.ComD.finalize(ComD.java:184)
    at dalvik.system.NativeStart.run(Native Method)

In my activity , i have the following code which lets me close all the network connection opened by the thread :

public void onPause() 
{
    if(this.myThread != null) {
        this.myThread.stopConnection();
    }
}

In my Thread :

public void run()
{
    this.setName("MessagesThread");
    if(this.initSocket())
    {

            Looper.prepare();
            this.threadHandler = initHandler();
            Looper.loop();
    }
    else
    {
        this.timeout();
    }
}

public void stopConnection()
{
    if(this.threadHandler != null) {
        this.threadHandler.removeMessages(ALIVE); // Remove a delayed message   
        this.threadHandler.getLooper().quit(); // Warning
    }
    this.connected = false;
    if(this.client != null) {
        this.client.close();
    }
}

private Handler initHandler()
{
    return new Handler() {

        public void handleMessage(Message msg)
        {
            switch(msg.what)
            {
                //Handling messages
            }
        }
    }
}

When i receive the warning "null sending message to a Handler on a dead thread" is that the activity trying to send a message to the thread or the oppposite ?

How can i fix this ?

Thanks

like image 640
grunk Avatar asked Aug 16 '11 09:08

grunk


1 Answers

You are getting the error as Looper.quit() has already been called.

So the message queue is basically unusable after Looper.quit() has been called the first time, as it enqueues a Message with a null target, which is the magical identifier for the message queue to stop enqueuing and appear "dead".

You need to do something like:

private boolean stoppedFlag= false;
public void stopConnection()
{
    if(this.threadHandler != null) {
        this.threadHandler.removeMessages(ALIVE); // Remove a delayed message   
        if(!stoppedFlag){
            this.threadHandler.getLooper().quit(); // Warning
            stopFlag = true;
        }
    }
    this.connected = false;
    if(this.client != null) {
        this.client.close();
    }
}

To stop quit() being called multiple times

Ref Looper

Ref Looper SOQ

like image 172
Blundell Avatar answered Nov 14 '22 23:11

Blundell