[SOLVED] Can’t toast on a thread that has not called looper.prepare()

13Aug, 2018

This particular error occurs mainly when working with threads in an Android project. Β  So straight to the point, no stories.

WHAT IT MEANS

I’m not going to go into all the technicalities of threading, what this error simply means is that you are trying to manipulate or show a User Interface component from a non-UI thread. Β  A simple example:

Running the above code will result in the “Can’t create handler inside thread that has not called Looper.prepare()” error.

SOLUTION

  1. Use activity.runOnUiThread(): Β  When manipulating or showing a UI component from a non UI Thread, simply use theΒ runOnUiThread() method. Β  For example:
  2. Use Looper: Β  Here’s another very simple solution:

There are many other solutions but I personally find these 2 very easy to implement.

 

REFERENCE

This StackOverflow question thought me a lot about this Android Error:
https://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare

Feel free to ask any questions or contribute to this post in the comment section below.


1 Comment

  • Ronald May 3, 2019 @ 8:24 am

    I love the way you described the cause of this Error.
    The first solution worked for me

Leave a Reply

Your email address will not be published. Required fields are marked *