The error has been made, but it is too early to be accustomed.
create handler inside thread that has not called Looper.prepare()
I have extracted the document and we can take a look at it.
This class is used to run message loops for threads. The default thread does not have a message loop associated with it, so you need to create one, call prepare () in the thread to run the loop, and then call Loop () to get process information at the end of the loop.
The most common interaction with the message loop is through the handler class.
The following is a typical instance of implementing a looper thread that creates an initial handler to interact with looper by separating the prepare () and loop () methods.
class looperthread extends thread { public Handler Mhandler; public void Run () {looper.prepare (); Mhandler = new Handler () {public void handlemessage (Message msg) {//process Incomi ng messages here }}; Looper.loop (); } }
In terms of the class name, Looper is the loop that executes messages (runnables) in this loop queue, as the usual threads do not have such circular queues. It is executed only once and is stopped after the execution of the method in the middle of prepare () and loop (). And this is managed by handler and does not block the UI thread. Looper is a message queue that is bundled into thread and manages this queue.
(Image from the network)
This picture shows the Looper life cycle, starting with prepare () and ending with Quit (). Loop () must also be called when creating Looper, and the infinite queue MessageQueue in the graph begins with loop ().
Android:can ' t create handler inside thread that have not called looper.prepare ()