java.lang.RuntimeException: Can’t create handler inside 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.


Leave a Reply

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