Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6346197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:58:19+00:00 2026-05-24T20:58:19+00:00

I have a thread using handler and messages to send data to the activity.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:58:20+00:00Added an answer on May 24, 2026 at 8:58 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing code in VS2005 using its STL. I have one UI thread
Is OutputDebugString(PAnsiChar('')); thread safe? I/we have been using it in threads for debugging, and
I have thread exception handler which saves the exception stack trace and should close
I have using following code to set a thread on button action. public void
Using POSIX threads & C++, I have an Insert operation which can only be
I have a Perl script that I'm attempting to set up using Perl Threads
I have one thread that writes results into a Queue. In another thread (GUI),
I have some thread-related questions, assuming the following code. Please ignore the possible inefficiency
I have a thread that, when its function exits its loop (the exit is
I have a thread that needs to be executed every 10 seconds. This thread

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.