1. Scrapped or attached views may not be recycled. isScrap:false isAttached:true
崩溃场景 : 一个普通的RecyclerView列表,点击某个item进入详情页,然后返回时崩溃(bug日志如下).发现比较难寻找到具体原因,于是搜索了大谷歌爸爸.
java.lang.IllegalArgumentException: Scrapped or attached views may not be recycled. isScrap:false isAttached:trueat android.support.v7.widget.RecyclerView$Recycler.recycleViewHolderInternal(RecyclerView.java:5659) at android.support.v7.widget.RecyclerView$Recycler.recycleView(RecyclerView.java:5603) at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:277) at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:324) at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:337) at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:344) at android.support.v7.widget.GapWorker.run(GapWorker.java:370) at android.os.Handler.handleCallback(Handler.java:743) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:150) at android.app.ActivityThread.main(ActivityThread.java:5665) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)复制代码
发现大部分导致这个bug的原因是:android:animateLayoutChanges与RecyclerView刷新共用.
大致意思是:这个错误的原因是在你的xml布局文件中将android:animateLayoutChanges 设置为了true 并且java 代码里对RecyclerView 的adapter 调用了notifyDataSetChanged() 方法。
会报错原因分析:android:animateLayoutChanges="true"是在列表增删Item的时候调用系统自带的动画效果,而RecyclerView的机制是对子视图的复用,不会在真正意义上对Item的控件进行增加、删除,与android:animateLayoutChanges原理相悖,因此会报错。另一条高赞回答显示 : 我们正在构建的RecyclerView有不止一种类型的视图,有些子view在有EditText时候。过了一会儿,我们将问题固定在与焦点有关的问题上。在回收EditTexts时,会发生此错误,当新的数据被绑定到一个循环的视图时,我们尝试清除焦点,但直到android:focusableInTouchMode =“true”被设置在RecycleView上才起作用。但是,我根本没有用到animateLayoutChanges这个属性,所以上面的对我来说并不适用.后来经过思考.发现我每次在返回重新刷新页面时候,发生的崩溃.排查代码发现每次刷新前会清空list,然后请求网络,而从清空数据到获取到数据的这段时间里,List中的数据是不存在的,所以给了RecyclerView要回收Item,但是View没有被回收的假象(此时并没有执行notifyDataSetChanged()方法),因此程序报错。其解决方法是:将列表清空的方法放到获取到接口数据以后执行。
第二种解决方案:
继承你要用的layoutmanager,捕获异常 代码如下:@Overridepublic void collectAdjacentPrefetchPositions(int dx, int dy, RecyclerView.State state, LayoutPrefetchRegistry layoutPrefetchRegistry) { try { super.collectAdjacentPrefetchPositions(dx, dy, state, layoutPrefetchRegistry); } catch (IllegalArgumentException e) { LogUtils.e("catch exception"); }}复制代码
2.Called attach on a child which is not detached: ViewHolder{41ce22c0 position=4 id=-1, oldPos=-1, pLpos:-1 no parent}
If you're using notifyItemChanged(position) try changing it for notifyDataSetChanged(). This could be a bug with notifyItemChanged. Try dispatching all events as soon as they happen as well, since RecyclerView already bundles the updates by default.