2

I want to use AsyncTask for update my db4o with a server. In the doInBackground method , I connect to the server, update the db4o, and schedule a pendingintents. Not modify UI or show any toast.

Initially, I had the following error:

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

After adding the Looper.prepare(), works fine, but only for five updates (AsyncTask). I've read this topic: AsyncTask threads never die (Android) , and I don't now that fails. When I throw the sixth update, the app crashes:

FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
at android.os.Looper.prepare(Looper.java:74)
(...)

five AsyncTask

I've read in the documentation that I need the Looper.loop(), but whith this, the app crashes..

Example:

protected Integer doInBackground(Void... params) {
    Looper.prepare();

        update = new Update();
        update.checknewObjects();
        update.deleteOldObjects();
        update.updateObjects();
        Looper.loop();
}

Why do I need Looper? Why the app crashes after five updates? Where can I schedule Looper.loop()?

Thanks in advance!

Community
  • 1
  • 1
Héctor Ortiz
  • 257
  • 1
  • 6
  • 17
  • 1
    "Caused by: java.lang.RuntimeException: Only one Looper may be created per thread" check if you are not using Looper more then one time on the same thread. Generally a Looper is needed if you are using Handlers inside threads (the main app thread has a Looper). http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/ – Raanan Oct 30 '12 at 14:22

3 Answers3

0

If you want to schedule tasks you should use Timer & TimerTask, from what I understand I think they should fit better to your needs. http://developer.android.com/reference/java/util/Timer.html

Raanan
  • 4,777
  • 27
  • 47
0

If you want to use AsyncTask so use it, you shouldn't mix it with Lopper...

protected Integer doInBackground(Void... params) {}

already working on background thread and there is no reason to call Lopper, for what?

If your application need to do some kind of long computation so AsyncTask is very good tool for it because offers methods which working on UI Thread and allow to update UI with some progress of work because every User that will use your application should know that application "doing something" (when it takes more than 2-5 seconds).

Also it's generic and that provide some benefits.

If you do not need to update UI just use for example Handler.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • Thanks deceiver.. but.. If Idon't use the looper I've the following error: Can't create handler inside thread that has not called Looper.prepare() – Héctor Ortiz Oct 30 '12 at 14:15
  • Your error means that you are trying to update your `UI` from background thread. this is generally not allowed. – Simon Dorociak Oct 30 '12 at 19:45
  • Looper can be used effectively in AsyncTask as well, in case u r using AsyncTask#executeOnExecutor() instead of #execute(), it puts your thread in another thread pool, which uses a different Handler AKA Looper. so u have to call Looper.prepare() – Masoud Dadashi Jan 02 '16 at 05:19
0

in asynck Task you can't use looping in background function .it giving error

Prabu
  • 1,441
  • 15
  • 20