Seraphis
Seraphis

Reputation: 1026

AsyncTask in Loop?

I'm trying to implement AsyncTask in my application. Problem is that it was created and executed from different thread, so I got exception. I moved forward and implement small runnable that will create and execute my AsyncTask. I run this runnable in runOnUIThread() method, but still got this error in my runnable's constructor, on line with AsyncTask constructor:

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

Any ideas what to do?

Need code?

myLocationOverlay.runOnFirstFix(new Runnable() {

            @Override
            public void run() {
                fillMap();
            }
        });

public void fillMap(){
            runOnUiThread(new AsyncTaskRunner());
}

private class AsyncTaskRunner implements Runnable {
        DownloadDataTask task;

        public AsyncTaskRunner(double latitude, double longitude, int radius) {
            super();

            this.task = new DownloadDataTask(latitude, longitude, radius);
        }

        @Override
        public void run() {
            task.execute();
        }
    }

Upvotes: 1

Views: 1705

Answers (1)

Sameer
Sameer

Reputation: 4389

The constructor of the AsyncTask is still being called on the non-UI thread. Can you move the construction of the AsyncTask to run method?

private class AsyncTaskRunner implements Runnable {
    double latitude;
    double longitude;
    int radius;

    public AsyncTaskRunner(double latitude, double longitude, int radius) {
        super();
        this.latitude = latitude;
        this.longitude = longitude;
        this.radius = radius;
    }

    @Override
    public void run() {
        new DownloadDataTask(latitude, longitude, radius).execute();
    }
}

Upvotes: 1

Related Questions