Android - Can't create handler inside thread that has not called Looper.prepare()

后端 未结 3 915
有刺的猬
有刺的猬 2020-12-29 06:04

I am developing a simple application using Google Maps V2 API just to get basics and I am facing this error:

09-09 21:21:41.154: E/AndroidRuntime(3796): FAT         


        
相关标签:
3条回答
  • 2020-12-29 06:47

    Do this in your do in background...

    runOnUiThread(new Runnable() {
        public void run() 
        {
            if (items.equals("null")) 
            {
                Toast.makeText(getApplicationContext(), "No Event Found",Toast.LENGTH_LONG).show();
            }                   
            else {                       
            }   
        }
    });
    
    0 讨论(0)
  • 2020-12-29 06:55

    You cannot execute an AsyncTask from a background thread. See the "Threading Rules" section of the AsyncTask documentation.

    0 讨论(0)
  • 2020-12-29 07:05

    I had a similar issue, problem was that I was having series of Thread from Main Thread. Actually Main UI Thread started a service, which ran on a service and subsequently started another thread, which finally had a FileObserver (another thread), I had to communicate to UI thread by popping a toast message. I tried for several hours, and tried below, it worked as a charm.

    //Let this be the code in your n'th level thread from main UI thread
    Handler h = new Handler(Looper.getMainLooper());
    h.post(new Runnable() {
      public void run() {
        Toast.makeText(context, "Your message to main thread", Toast.LENGTH_SHORT).show();
      }
    });
    

    The main key here is the getMainLooper() function of Looper class, which will provide you the Looper against the Main UI thread.

    0 讨论(0)
提交回复
热议问题