5

I know, this is again a repeated question but my case it is different question.

I have a class abc with a static function & a Handler. Earlier i couldn't able call handler from a static function. Then i googled for Access a non-static function from a static function & found an solution is to create an instance of class & access non-static variable. But now, why, i m getting this error.

E/AndroidRuntime(13343): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

public class abc
 {    
    static public void Instantiate()
    {
         abc xyz = new abc();
         xyz.handler.sendEmptyMessage(1);      **//GETTING ERROR IN THIS LINE**
    }

    public Handler handler = new Handler() 
        {
                public void handleMessage(Message msg) 
                {
                        switch (msg.what)
                        {
                        }
                 }
        }

}

My Question: How can i send message to handler from a static function?

Thankx.

MicroEyes
  • 3,680
  • 4
  • 26
  • 35
  • from where are you calling this `Instantiate()` method? Generally you are supposed to call it from an Async Task/ worker thread (the one other than the main UI Thread). – PrincessLeiha Jul 17 '12 at 09:11
  • I'd suggest you tell us what you're *really* trying to do rather. We might be abe to help you work around this. – Philippe Girolami Jul 17 '12 at 09:53

3 Answers3

7

check the place where you are doing this:

abc.Instantiate();

and replace it with

runOnUiThread(new Runnable() {

    @Override
    public void run() {
        abc.Instantiate();
    }
});

I hope you're calling it from an Activity

Some Explanation (quoting bicska88) :)


What causes the problem, doesn't have anything to do with the fact that you send a message to a Handler object from within a static function. The problem is that you send a message to the handler from a thread that has not called Looper.prepare() (as the error message says, the thread doesn't have a message loop). This may be fixed by explicitly calling Looper.prepare() before-while, or by running the code on the UIThread.


Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • Note that this will not work, since the newly created thread doesn't have a message loop associated with it, and will still throw the above mentioned exception. – overbet13 Jul 17 '12 at 09:51
  • hehehee.. Thankx @bicska88.. but magically its worked for me.. Thankx for your answer Sherif. Atleast i got a clue of what i shud in my application. – MicroEyes Jul 17 '12 at 11:06
  • It is working because the UI thread **HAS** a message loop. Note that my answer included a fully-working code sample. – overbet13 Jul 18 '12 at 05:41
  • This answer is not correct, since the error message already tells us that you have created the handler on a thread that is different from the UI thread. The `Handler` object belongs to the thread where you created it. This way you create it on the UI thread, so the `Handler` object belongs to this thread. If you wanted this answer, then your question is ambiguous, inexact and indistinctive. Read the documentation and probably you will see my point. – overbet13 Jul 18 '12 at 06:04
2

try defining the handler as

final static Handler handler = new Handler() { ... };
mihail
  • 2,173
  • 19
  • 31
2

What causes the problem, doesn't have anything to do with the fact that you send a message to a Handler object from within a static function. The problem is that you send a message to the handler from a thread that has not called Looper.prepare() (as the error message says, the thread doesn't have a message loop). To fix this, do the following:

public class abc
{    
    public Handler handler;

    static public void Instantiate()
    {
        abc xyz = new abc();
        Looper.prepare();
        handler = new Handler() 
        {
            public void handleMessage(Message msg) 
            {
                switch (msg.what)
                {

                }
            }
        }
        xyz.handler.sendEmptyMessage(1);
        Looper.loop();
    }
}

Documentation can be found at this link.

overbet13
  • 1,654
  • 1
  • 20
  • 36