2013-01-13 76 views
0

我正在ListView中顯示一些RSS數據。我正在閱讀Asynchronous任務類提供的onProgressUpdate()方法。使用RSS數據更新列表

我想知道是否有可能使用此方法來更新列表視圖,因爲每個項目從XML中檢索?

我overrided這裏的方法:

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

    } 

我收到此錯誤:android.view.ViewRootImpl $ CalledFromWrongThreadException:

所致只有創建視圖層次可以觸摸原始線程其觀點。

任何想法我錯了嗎?

編輯:我剛剛讀了一篇文章,說這個錯誤是從嘗試更新doInBackground()方法內的後臺線程中的UI線程生成的。在我的情況下,我在doInBackground()方法中調用onProgressUpdate()。我試圖在下載每個項目時添加列表的效果。

任何有關如何實現這一目標的建議都會很棒!

登錄貓:

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 
+0

添加完整的StackTrace。 –

回答

2

In my case I am calling the onProgressUpdate() in the doInBackground() method.

你必須調用publishProgress()代替。如果你直接調用onProgressUpdate(),那仍然是從後臺線程運行,所以會失敗。

你應該看看AsyncTask documentation,他們提供了一個例子。

+0

謝謝你做到了! – Javacadabra