0

我正在努力在 ListView 中显示一些 RSS 数据。我正在阅读异步任务类提供的 onProgressUpdate() 方法。

我想知道是否可以使用此方法在检索 XML 中的每个项目时更新列表视图?

我在这里重写了方法:

    @Override
    protected void onProgressUpdate(Integer... values) {
        adapter.notifyDataSetChanged();

    }

我收到此错误:

引起:android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸它的视图。

有什么想法我哪里出错了吗?

编辑:我刚刚读到一篇文章,说这个错误是由于试图从doInBackground()方法内部的后台线程中更新 UI 线程而产生的。就我而言,我onProgressUpdate()doInBackground()方法中调用了。我试图在下载每个项目时添加列表填充的效果。

任何关于我如何实现这一目标的建议都会很棒!

日志猫:

01-13 00:41:09.195: E/AndroidRuntime(32239): FATAL EXCEPTION: AsyncTask #1
01-13 00:41:09.195: E/AndroidRuntime(32239): java.lang.RuntimeException: An error occured while executing doInBackground()
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at java.lang.Thread.run(Thread.java:856)
01-13 00:41:09.195: E/AndroidRuntime(32239): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.view.View.requestLayout(View.java:15468)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.view.View.requestLayout(View.java:15468)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.view.View.requestLayout(View.java:15468)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.view.View.requestLayout(View.java:15468)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.view.View.requestLayout(View.java:15468)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.widget.AbsListView.requestLayout(AbsListView.java:1819)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:813)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:5958)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at com.example.simplerss.MainActivity$PostTask.onProgressUpdate(MainActivity.java:148)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at com.example.simplerss.MainActivity$PostTask.doInBackground(MainActivity.java:123)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at com.example.simplerss.MainActivity$PostTask.doInBackground(MainActivity.java:1)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-13 00:41:09.195: E/AndroidRuntime(32239):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-13 00:41:09.195: E/AndroidRuntime(32239):    ... 4 more
4

1 回答 1

2

就我而言,我在 doInBackground() 方法中调用 onProgressUpdate()。

你必须打电话publishProgress()。如果您直接调用onProgressUpdate(),那仍然从后台线程运行,因此将失败。

您应该查看 AsyncTask文档,它们提供了一个示例。

于 2013-01-13T00:42:29.140 回答