How to fix sending a null message to a dead thread warning handler?

I have a thread that uses a handler and messages to send data to an action. Everything works fine, except when the action 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 that allows me to close the entire network connection opened by the stream:

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

In my topic:

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 get the warning "send message" null for handler in a dead thread "is this an action that tries to send a message to a stream or oppposite?

How can i fix this?

thank

+5
source share
1 answer

You get an error because Looper.quit () is already called.

, , Looper.quit() , , , "".

- :

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();
    }
}

quit()

Ref Looper

Ref Looper SOQ

+6

All Articles