Asynctask and Looper.Prepare() Error

AsyncTask and Looper.prepare() error

Long story:

AsyncTask internally uses a Handler. A handler basically allows you to post Runnables from another thread on the thread the handler was assigned to, which in the case of AsyncTask is always the thread from which it is called. This only works for threads that have a Looper prepared, though.

For more information see http://developer.android.com/reference/android/os/Handler.html

Short story:

Simply wrap every call to FinderMain$1.gotLocation or the creation of AsyncTask within it in a Runnable, and post it to a Handler bound to the UI thread, like this:

class GetLastLocation extends TimerTask {
private Handler mHandler = new Handler(Looper.getMainLooper());

@Override
public void run() {
// ...
mHandler.post(new Runnable() {
public void run() {
locationResult.gotLocation(null);
}
});
// ...
}
}

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

You are attempting to update the UI from a background thread. Either move the toast to onPostExecute, which executes on the UI thread (recommended), or call runOnUiThread.

runOnUiThread(new Runnable() {
public void run() {
// runs on UI thread
}
});

Android Looper.prepare() and AsyncTask

What is really going on is you are attempting to perform something on a background thread that requires the UI thread to run.

The Looper is a part of the system that ensures that transactions are done in order, and the device is responding as it should.

95% of the time when you get the Looper error, what it really means is you need to move part of your code to the UI thread, in Asynctask this means moving it to either onPostExecute or onProgressUpdate.

In your case it appears as if you are adding views, which is part of the UI and therefor would cause a problem. If that is not in fact what is causing the problem, an examination of the stacktrace should give you some clues.

As a side note, if you MUST call Looper.prepare() I would call it at the beginning of your thread. However, this is generally recommended to avoid the need to call it.

Looper.prepare() error inside a thread

Add Looper.prepare() and Looper.loop() in you code, like this:

private void GetGCM(final String UserID) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Looper.prepare();
GCMHelper gcmRegistrationHelper = new GCMHelper(getApplicationContext());
String gcmRegID = "";
gcmRegID = gcmRegistrationHelper.GCMRegister("123456");

// Update using Web Service
try {
UpdateGCMWSTask updateGCMWSTask = new UpdateGCMWSTask();

updateGCMWSTask.execute(UserID, gcmRegID);
// ************ HERE IS THE ERROR ***********************

}catch (Exception e)
{
e.printStackTrace();
}

Looper.loop();
} catch (Exception bug) {
bug.printStackTrace();
}
}
});

thread.start();
}

Android AsyncTask Error [Can't create handler inside thread that has not called Looper.prepare()]

You can't do anything in UI thread from async task, probably some of your functions inside asynctask are calling UI Thread, you can call them onPreExecute, onPostExecute, or use publishProgress to call UI thread while asynctask is running.

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

Seems you are doing some stuff to Update your UI from a Non-UI thread. So, you should put your stuff to update the UI inside runOnUiThread() so that it executes the stuff on your UI hread itself.

UPDATE:

Just try to put your stuff for getting CheckCountryPresence(); inside onPreExecute(). So your edited class should be like,

class checkCountryAsync extends AsyncTask<String, String, String> {

int countryPresence = 0;
@Override
protected void onPreExecute() {
countryPresence = localDatabase.CheckCountryPresence();
}
@Override
protected String doInBackground(String... aurl) {

Activity_name.this.runOnUiThread(new Runnable() {

@Override
public void run() {
if (countryPresence == 0)
{
CountryTable country = new CountryTable() ;
country.EnterCountry();
}
}
});
return null;
}
}

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

Probablly you called AsyncTask.execute() in a background thread.

You can only execute AsyncTask in UI thread.

Why do I need a Looper in my AsyncTask?

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.



Related Topics



Leave a reply



Submit