循环中的异步任务?

发布于 2025-01-01 23:20:34 字数 933 浏览 1 评论 0原文

我正在尝试在我的应用程序中实现AsyncTask。问题是它是从不同的线程创建和执行的,所以我得到了异常。我继续前进并实现了小型可运行程序,它将创建并执行我的 AsyncTask。我在 runOnUIThread() 方法中运行此可运行对象,但在可运行对象的构造函数中仍然遇到此错误,与 AsyncTask 构造函数一致:

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

有什么想法吗?

需要代码吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[旋木] 2025-01-08 23:20:34

AsyncTask 的构造函数仍在非 UI 线程上调用。您可以将 AsyncTask 的构造移至 run 方法吗?

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

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文