Karan
Karan

Reputation: 497

RuntimeException in AsyncTask

I am using AsyncTask for a long running task but I am getting the following error.

error in doInBackground====java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

Why am I getting that error and what does it mean?

Upvotes: 0

Views: 1378

Answers (4)

hqt
hqt

Reputation: 30284

Because you don't post your code, so hard to find issue.

I think because you access UI Thread inDoInBackground. (for example: you access TextView, or Button, or EditText....) This is a most common error when use Asyntask.

you should post some parameter to publishProgress in DoInBackGround. then asyntask will send this param to onProgressUpdate and this method will act on UI Thread

Upvotes: 0

bigstones
bigstones

Reputation: 15267

You're probably running the task inside a non-UI thread.

The UI thread is set so that it loops. You can see where's the loop in a stack trace or in ddms while debugging:

...
MessageQueue.next() line: 146
Looper.loop() line: 110
...

This loop takes care of handling the messages, like the ones that an AsyncTask sends, with a Handler, like the one the AsyncTask is trying to set. If you're launching the task on a thread that has no looper set you get that message.

Upvotes: 1

Farhan
Farhan

Reputation: 13390

Usually this error comes when you change UI from asyntask which is not allowed, other way is to implement handler instead asyntask...

Upvotes: 0

Lukas
Lukas

Reputation: 465

You can access GUI only from OnPostExecute and OnProgressUpdate.

Upvotes: 1

Related Questions