5

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

grunk
  • 14,718
  • 15
  • 67
  • 108
  • I thought you wanted to send data from the worker thread to the activity (UI thread)? If so, shouldn't the `Handler` be in the `Activity` while the worker thread `sendMessage` to it? Sorry if I misunderstood your logic – romy_ngo Aug 19 '11 at 09:53
  • The worker thread send data to the activity indeed. But the activity ask this data (and other stuff) to the worker thread. So both of them have handler and messages – grunk Aug 19 '11 at 09:57

1 Answers1

6

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

Community
  • 1
  • 1
Blundell
  • 75,855
  • 30
  • 208
  • 233