yshak
yshak

Reputation: 2201

Issue using Async Task

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();
    }

}

Upvotes: 0

Views: 1974

Answers (2)

rDroid
rDroid

Reputation: 4945

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.

Upvotes: 1

Lalit Poptani
Lalit Poptani

Reputation: 67296

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);
    }
});

Upvotes: 3

Related Questions