我基本上使用开发人员的源代码'培训课程(Training)。我尝试使用带有像src这样的片段的动态UI来阅读Android的RSS源。 并使用MainActivity中的新线程来分阶段.XML。 这是我使用的代码。(code)
我更改了数据类型:
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>();
}
有时效果很好。但是当触摸headlines_fragment的列表项时,通常崩溃。所以我无法阅读其中的详细文章。
这是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)
答案 0 :(得分:0)
您可能正在从Main(UI)以外的其他线程更新UI元素。它会在某个时间但不是所有时间都有效。请参阅此链接Can you access UI elements from another thread? (get not set)。
答案 1 :(得分:0)
例外情况是,当您不在主UI线程中时,您正尝试更新列表。
从Android 3.0开始,您需要从主线程外部执行网络操作,因此我假设您在后台线程中解析RSS源,然后尝试从同一线程更新ListView。
在后台线程中,一旦解析了RSS提要,就应该像这样更新List:
ArticleListAdapter adapter = new ArticleListAdapter(activityContext, articles);
articleListFrag.setListAdapter(adapter);
adapter.notifyDataSetChanged();
其中articles
是您已解析的新列表或文章,activityContext
是ListView所属的活动(这应该在AsyncTask
启动时初始化
Android RSS Application tutorial + code
我最近写了一篇关于为android创建RSS解析器的教程 - 并且如果要查看它,已将所有源代码放在GitHub上。关于后台线程解析RSS和更新列表视图,您可以主要参考this class