eventbus发送消息刷新上一页listview 消息接收不到及返回上一页时数组角标越界

时间:2024-04-10 09:08:46

问题一:当前activity发送消息至上一页面,消息未被接收(消息发送没有问题)

eventbus发送消息刷新上一页listview 消息接收不到及返回上一页时数组角标越界

解决:由于eventbus用的不多,觉得简单使用不难就忽略了生命周期问题,上图中我照用别人的代码,将反注册放在activty和fragment中的onStop()方法中,导致页面跳转时消息还未发送和接收eventbus就已取消注册(即事件的订阅),都已取消注册了消息自然是无法接收的,解决也很简单,将EventBus.getDefault().unregister(this)这行放入页面的onDestroy()方法中即可(汗 错误很低级,但要是你没注意到生命周期这个点,谷歌百度都没法破,因为太低级了)


问题二:消息发送成功,我重新网络请求刷新页面数据,结果返回上一页面时闪退报角标越界(报的是底层的角标越界,并没有直接标记我们写的代码)。

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.baibeiyun.beautymasterteacher, PID: 25680
                                                                                   java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
                                                                                       at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                                       at java.util.ArrayList.get(ArrayList.java:308)
                                                                                       at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
                                                                                       at android.widget.ListView.dispatchDraw(ListView.java:3329)
                                                                                       at android.view.View.draw(View.java:16204)
                                                                                       at android.widget.AbsListView.draw(AbsListView.java:4217)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:15198)
                                                                                       at android.view.View.draw(View.java:15971)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3612)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3402)
                                                                                       at android.view.View.draw(View.java:16204)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:15198)
                                                                                       at android.view.View.draw(View.java:15971)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3612)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3402)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:15193)
                                                                                       at android.view.View.draw(View.java:15971)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3612)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3402)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:15193)
                                                                                       at android.view.View.draw(View.java:15971)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3612)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3402)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:15193)
                                                                                       at android.view.View.draw(View.java:15971)
                                                                                       at android.view.ViewGroup.drawChild(ViewGroup.java:3612)
                                                                                       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3402)
                                                                                       at android.view.View.draw(View.java:16204)
                                                                                       at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)
                                                                                       at android.view.View.updateDisplayListIfDirty(View.java:15198)
                                                                                       at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:283)
                                                                                       at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:289)
                                                                                       at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:324)
                                                                                       at android.view.ViewRootImpl.draw(ViewRootImpl.java:2642)
                                                                                       at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2461)
                                                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2094)
                                                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
                                                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6050)
                                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
                                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:672)
                                                                                       at android.view.Choreographer.doFrame(Choreographer.java:608)
                                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
                                                                                       at android.os.Handler.handleCallback(Handler.java:742)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5528)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:740)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:630)

解决:挺怕这种报错的,因为你没法直接知道具体你哪一行代码有问题,无头苍蝇似的找bug确实是很痛苦的,顺着思路想是这样的---既然角标越界那就是你代码中一定有操作角标的行为,找了下当前的activity中虽然并没有直接操作角标的代码,但他有使用listview有listview就会有adapter,而adapter的界面显示就是按着数据源的角标取值后显示的,那么基本可以暂定凶手就是adapter了,本案例中由于自家后台返回请求数据时,网络请求成功但确实没数据和请求失败没数据这两者缺乏一个合理标识,所以我在网络请求返回无数据的情况下根据当前请求是刷新还是加载更多操作来确定是否清空list数据源,问题就出在当清空情况下我疏忽少了一句adapter.notifyDataSetChanged操作(真心发现很多的疑难杂症往往都源于自己的一个不经意疏忽),造成明明数据已经清空了,但adapter没有刷新还是照旧拿角标取数据显示,自然就角标越界了,解决同样简单,加上notifyDataSetChanged刷新操作就是了,教训是凡是数据源数据有增删时adapter一定别漏了刷新操作。(第一次写日记,竟然全是些低级错误,汗啊,怀疑自己的打码水平 >_<|||)