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

后端 未结 3 937
有刺的猬
有刺的猬 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 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.

提交回复
热议问题