0

I basiclly use source codes of Developers' training courses (Training).I try to read a RSS feed into Android with dynamic UI with fragments like src. And use new thread in MainActivity to phase .XML. here is the code I use.(code)

I change the Data type:

    public class RSSData {

//  public static String[] Headlines = new String[20];
    public static List<String> Headlines = new ArrayList<String>();

//  public static String[] Articles = new String[20];
    public static List<String> Articles = new ArrayList<String>();
}

It works well sometimes. But often crash when touching list items of headlines_fragment. So I can't read detail articles in them.

Here is logcat:

03-16 17:19:43.102: E/InputEventReceiver(12106): Exception dispatching input event.
03-16 17:19:43.102: E/MessageQueue-JNI(12106): Exception in MessageQueue callback: handleReceiveCallback
03-16 17:19:43.132: E/MessageQueue-JNI(12106): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.widget.ListView.layoutChildren(ListView.java:1544)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:3953)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.View.dispatchTouchEvent(View.java:7350)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2412)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2147)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2139)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1476)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2087)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.View.dispatchPointerEvent(View.java:7535)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3415)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3347)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4456)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4434)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4538)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.os.MessageQueue.nativePollOnce(Native Method)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.os.MessageQueue.next(MessageQueue.java:125)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.os.Looper.loop(Looper.java:124)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at android.app.ActivityThread.main(ActivityThread.java:4921)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at java.lang.reflect.Method.invokeNative(Native Method)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at java.lang.reflect.Method.invoke(Method.java:511)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
03-16 17:19:43.132: E/MessageQueue-JNI(12106):  at dalvik.system.NativeStart.main(Native Method)
03-16 17:19:43.147: D/AndroidRuntime(12106): Shutting down VM
03-16 17:19:43.147: W/dalvikvm(12106): threadid=1: thread exiting with uncaught exception (group=0x41b212a0)
03-16 17:19:43.177: E/AndroidRuntime(12106): FATAL EXCEPTION: main
03-16 17:19:43.177: E/AndroidRuntime(12106): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.widget.ListView.layoutChildren(ListView.java:1544)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3953)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.View.dispatchTouchEvent(View.java:7350)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2412)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2147)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2139)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1476)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2087)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.View.dispatchPointerEvent(View.java:7535)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3415)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3347)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4456)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4434)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4538)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.os.MessageQueue.nativePollOnce(Native Method)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.os.MessageQueue.next(MessageQueue.java:125)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.os.Looper.loop(Looper.java:124)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at android.app.ActivityThread.main(ActivityThread.java:4921)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at java.lang.reflect.Method.invokeNative(Native Method)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at java.lang.reflect.Method.invoke(Method.java:511)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-16 17:19:43.177: E/AndroidRuntime(12106):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Community
  • 1
  • 1
Dan
  • 301
  • 3
  • 21

2 Answers2

0

Probably you are updating your UI elements from another thread than Main(UI).It will work sometime but not all time.Refer this link Can you access UI elements from another thread? (get not set).

Community
  • 1
  • 1
vinoth
  • 485
  • 4
  • 16
  • I can't find the answer there. Maybe I don't understand dispatcher well. In debug mode, I can sure arraylist, `Headlines` and `Articles`, get right values. Hope you can provide more descriptions. – Dan Mar 16 '13 at 12:04
0

The exception is saying that you are attempting to update the list when not within the main UI thread.

Since Android 3.0, you have been required to perform network operations from outside of the main thread, so I assume you are parsing the RSS feed in a background thread and then trying to update the ListView from the same thread.

In the background thread, once you have parsed the RSS feed you should update the List something like this:

ArticleListAdapter adapter = new ArticleListAdapter(activityContext, articles);
articleListFrag.setListAdapter(adapter);
adapter.notifyDataSetChanged();

Where articles is the new list or articles that you have parsed and activityContext is the activity that the ListView is a part of (this should be initialised when the AsyncTask is kicked off.

Android RSS Application tutorial + code

I have recently written up a tutorial on creating an RSS parser for android - and have put all the source code on GitHub if you want to check it out. With regards to the background thread parsing the RSS and updating the list view you can mainly refer to this class

rhinds
  • 9,976
  • 13
  • 68
  • 111
  • OK. Let me try it for a while. btw I download your project from GitHub and try to run it. But it crashes at the beginning after import support v4 jar file. – Dan Mar 21 '13 at 05:45
  • What happened with the crash? Your IDE froze/closed/etc? What IDE/setup etc? How did you import the files? – rhinds Mar 21 '13 at 09:59
  • logcat: `03-21 21:46:36.355: E/AndroidRuntime(11573): FATAL EXCEPTION: main 03-21 21:46:36.355: E/AndroidRuntime(11573): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nerdability.android/com.nerdability.android.ArticleListActivity}: java.lang.ClassNotFoundException: com.nerdability.android.ArticleListActivity 03-21 21:46:36.355: E/AndroidRuntime(11573): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034) ` device: i9100 4.1.2 I add the jar file with "Java build path">"Add JAR" – Dan Mar 21 '13 at 13:49