0

我基本上使用开发人员培训课程(培训)的源代码。我尝试将 RSS 提要读入带有动态 UI 的 Android 中,并带有像 src 这样的片段。并在 MainActivity 中使用新线程来分阶段 .XML。这是我使用的代码。(代码

我更改数据类型:

    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>();
}

有时效果很好。但是在触摸 headers_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)
4

2 回答 2

0

例外情况是当您不在主 UI 线程中时尝试更新列表。

从 Android 3.0 开始,您需要从主线程外部执行网络操作,因此我假设您在后台线程中解析 RSS 提要,然后尝试从同一线程更新 ListView。

在后台线程中,一旦你解析了 RSS 提要,你应该像这样更新列表:

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

articles您已解析的新列表或文章在哪里,并且activityContext是 ListView 所属的活动(这应该在启动时初始化AsyncTask

Android RSS 应用教程+代码

我最近写了一篇关于为 android 创建 RSS 解析器的教程——如果你想查看的话,我已经把所有的源代码放在了 GitHub 上。关于后台线程解析RSS和更新列表视图主要可以参考这个类

于 2013-03-19T22:04:23.047 回答
0

可能您正在从 Main(UI) 之外的另一个线程更新您的 UI 元素。它会在某个时候但不是一直有效。请参阅此链接您可以从另一个线程访问 UI 元素吗?(不设置)

于 2013-03-16T09:33:17.967 回答