Android:后台的 SQLiteCursor/SimpleCursorAdapter

发布于 2024-10-19 20:54:08 字数 1183 浏览 1 评论 0原文

我的问题与 Android 有关。我有以下代码(省略了不相关的部分):

public class MyActivity extends ListActivity {
    protected void onResume() {
        super.onResume();
        new UpdateTask().execute();
    }

    private class UpdateTask extends AsyncTask<Object, Object, ListAdapter> {
        protected ListAdapter doInBackground(Object... params) {
            SQLiteCursor cursor = db.getSomeData();
            startManagingCursor(cursor);
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, ..., cursor, ..., ...);
            return adapter;
        }

        protected void onPostExecute(ListAdapter result) {
            listView.setAdapter(result);
        }
    }
 }

这会导致 RuntimeException - 在实例化 SimpleCursorAdapter 的行处“无法在未调用 Looper.prepare() 的线程内创建处理程序”。

我明白为什么会发生这种情况,我只是不知道如何修复它(将其保留在单独的线程中)。我找到了这个帖子:

http://groups.google .com/group/android-developers/browse_thread/thread/34d0069bb2de925e?fwc=2

但我不太明白回复。我无法通过谷歌搜索任何 涉及 SQLiteCursor 和 AsyncQueryHandler 的示例。

你们能帮忙吗?谢谢。

My problem is related to Android. I have the following code (non-relevant parts omitted):

public class MyActivity extends ListActivity {
    protected void onResume() {
        super.onResume();
        new UpdateTask().execute();
    }

    private class UpdateTask extends AsyncTask<Object, Object, ListAdapter> {
        protected ListAdapter doInBackground(Object... params) {
            SQLiteCursor cursor = db.getSomeData();
            startManagingCursor(cursor);
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, ..., cursor, ..., ...);
            return adapter;
        }

        protected void onPostExecute(ListAdapter result) {
            listView.setAdapter(result);
        }
    }
 }

This causes a RuntimeException - "Can't create handler inside thread that has not called Looper.prepare()" at the line where SimpleCursorAdapter is instantiated.

I understand why this is happening, I just don't know how to fix it (keeping this in a separate thread). I have found this thread:

http://groups.google.com/group/android-developers/browse_thread/thread/34d0069bb2de925e?fwc=2

But I don't really understand the reply. I have failed to google any
example involving SQLiteCursor and AsyncQueryHandler.

Could you guys please help? Thanks.

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

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

发布评论

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

评论(2

浅浅淡淡 2024-10-26 20:54:08

看起来 SimpleCursorAdapter 的实例化必须在 UI 线程上进行。您可以通过让 doInBackground() 返回 cursor 并在 onPostExecute() 中设置适配器来实现此目的。

如果您还没有看过无痛线程,请参阅。

It looks like instantiating the SimpleCursorAdapter has to happen on the UI thread. You could do that by having doInBackground() return cursor and setting up the adapter in onPostExecute().

See Painless Threading if you haven't already.

甜柠檬 2024-10-26 20:54:08

只需放入 Looper.prepare();在抛出此类错误的方法的顶部

just put Looper.prepare(); at the top of the method that throws such an error

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