Android:后台的 SQLiteCursor/SimpleCursorAdapter
我的问题与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来
SimpleCursorAdapter
的实例化必须在 UI 线程上进行。您可以通过让doInBackground()
返回cursor
并在onPostExecute()
中设置适配器来实现此目的。如果您还没有看过无痛线程,请参阅。
It looks like instantiating the
SimpleCursorAdapter
has to happen on the UI thread. You could do that by havingdoInBackground()
returncursor
and setting up the adapter inonPostExecute()
.See Painless Threading if you haven't already.
只需放入 Looper.prepare();在抛出此类错误的方法的顶部
just put Looper.prepare(); at the top of the method that throws such an error