使用RSS数据更新列表

时间:2013-01-13 00:35:34

标签: android rss android-listview android-asynctask

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

我想知道是否可以使用此方法更新列表视图,因为XML中的每个项都被检索到了?

我在这里覆盖了这个方法:

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

    }

我收到此错误:

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

我出错的任何想法?

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

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

Log cat:

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

1 个答案:

答案 0 :(得分:2)

  

在我的情况下,我在doInBackground()中调用onProgressUpdate()   方法

您必须拨打publishProgress()。如果您直接调用onProgressUpdate(),那仍然是从后台线程运行的,那么将会失败。

你应该看看AsyncTask documentation,它们提供了一个例子。