0

I am using async task in my application.I am starting a progress dialog on pre-execute and in doInBackground i'm calling a function which will return the current location from geocoder.If the current place is not available i'm showing a dialog box to manually enter the place.But it is getting crashed when the dialog comes.i'm not even able to use a toast in that function which results a force close.Can any1 have the solution for this issue..? Please help.. This is the error that i'm getting..

01-03 09:45:36.216: E/AndroidRuntime(452): FATAL EXCEPTION: AsyncTask #1
01-03 09:45:36.216: E/AndroidRuntime(452): java.lang.RuntimeException: An error  occured while executing doInBackground()
01-03 09:45:36.216: E/AndroidRuntime(452):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-03 09:45:36.216: E/AndroidRuntime(452):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
01-03 09:45:36.216: E/AndroidRuntime(452):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
01-03 09:45:36.216: E/AndroidRuntime(452):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
01-03 09:45:36.216: E/AndroidRuntime(452):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-03 09:45:36.216: E/AndroidRuntime(452):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-03 09:45:36.216: E/AndroidRuntime(452):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-03 09:45:36.216: E/AndroidRuntime(452):  at java.lang.Thread.run(Thread.java:1019)
01-03 09:45:36.216: E/AndroidRuntime(452): Caused by: java.lang.RuntimeException:     Can't create handler inside thread that has not called Looper.prepare()
01-03 09:45:36.216: E/AndroidRuntime(452):  at android.os.Handler.<init>(Handler.java:121)
01-03 09:45:36.216: E/AndroidRuntime(452):  at android.widget.Toast.<init>(Toast.java:68)
01-03 09:45:36.216: E/AndroidRuntime(452):  at android.widget.Toast.makeText(Toast.java:231)

my async task is this.

private class UpdateCity extends AsyncTask<String, Void, Void> {
    ProgressDialog dialog = new ProgressDialog(this);



    @Override
    protected void onPreExecute() {

        this.dialog.setMessage("Please wait for few seconds...");
        this.dialog.setTitle("");
        this.dialog.show();

    }

    @Override
    protected Void doInBackground(final String... args) {

        checkCity(Cplacename);//my function
        return null;

    }

    @Override
    protected void onPostExecute(final Void unused) {


        this.dialog.dismiss();
    }

}
4
  • 3
    You gave us no code and no stacktrace from logcat...how are we to help you?
    – Will Tate
    Commented Jan 3, 2012 at 4:10
  • 1
    Can you add your async task code as well, I guess you will need to call the Looper.prepare before the thread starts. As specified in the error.
    – nala4ever
    Commented Jan 3, 2012 at 4:22
  • I want to know how to handle the exceptions from a function which is executing in doInBackground.For example: a toast
    – yshak
    Commented Jan 3, 2012 at 4:24
  • If you are updating UI for eg. Toast, then you have to use runOnUiThread() to peform anything on the UI, you directly can't upate the UI from the worker thread. see my answer below. Commented Jan 3, 2012 at 4:32

2 Answers 2

3

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

From the above error in your Logcat it seems that you are trying to upate the UI from a non-UI thread. You should use runOnUiThread() to update your UI from a non-UI thread.

Activity_name.this.runOnUiThread(new Runnable() {
    public void run() {
     checkCity(Cplacename);
    }
});
4
  • Thankz...it seems that is the problem...so where should i write the above code? in Async task?
    – yshak
    Commented Jan 3, 2012 at 4:36
  • 1
    check my edited answer, just put your method checkCity(Cplacename); inside the runOnUiThread(). Commented Jan 3, 2012 at 4:37
  • If you run this code on UI thread, the complete AsyncTask is useless. I would prefer the solution from rDroid. Commented Jan 3, 2012 at 9:03
  • checkCity is not connecting to some web service, or doing some database query, is it ? if so, do not perform a heavy operation in the UI thread.
    – rDroid
    Commented Jan 4, 2012 at 9:13
1

THe latter part of the logcat referes to some Toast . Are you showing some Toast from within the doInBackground() function ? If some, remove that, and it should work. Do remember, Toast is a UI element.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.