Reputation: 5768
I am working on displaying some RSS data in a ListView. I was reading up on the onProgressUpdate() method provided by the Asynchronous task class.
I was wondering if it would be possible to use this method to update the listview as each item from XML is retrieved?
I overrided the method here:
@Override
protected void onProgressUpdate(Integer... values) {
adapter.notifyDataSetChanged();
}
I am getting this error:
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Any ideas where I am going wrong?
EDIT: I just read an article saying this error is generated from trying to update the UI thread from within the background thread inside of the doInBackground()
method. In my case I am calling the onProgressUpdate()
in the doInBackground()
method. I was trying to add the effect of the list populating as each item was downloaded.
Any suggestions of how I could achieve this would be great!
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
Upvotes: 0
Views: 112
Reputation: 36449
In my case I am calling the onProgressUpdate() in the doInBackground() method.
You have to call publishProgress()
instead. If you directly call onProgressUpdate()
, that is still running from the background thread and so, will fail.
You should take a look at the AsyncTask documentation, they provide an example.
Upvotes: 2