- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我们的 QA 检测到一个错误:发生了以下与 RecyclerView 相关的崩溃:`
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 2(offset:2).state:3
一个残酷的解决方法可能是在异常发生时捕获异常并从头开始重新创建 RecycverView 实例,以避免留下损坏的状态。
但是,如果可能的话,我想更好地理解问题(并可能从源头上解决它),而不是掩盖它。
该错误很容易重现,滚动继续快速导致此崩溃,但一旦发生,它是致命的。
我的工作代码是:
public class MoviesGridRecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int VIEW_ITEM = 1;
public static final int VIEW_PROG = -1;
private static final int FADE_DURATION = 500;
int pastVisiblesItems, visibleItemCount, totalItemCount;
private List<VideoEntity> dataList;
private FragmentActivity activity;
private int visibleThreshold = 1;
//private int lastVisibleItem, totalItemCount;
//private boolean loading = false;
private OnLoadMoreListener onLoadMoreListener;
private RecyclerView.OnScrollListener recyclerciewScrollListener;
private RecyclerView recyclerView;
private boolean followLargeItemOnDataRender;
private boolean loading = false;
private boolean isLiveChannel = false;
public MoviesGridRecyclerViewDataAdapter(FragmentActivity fragmentActivity, final List<VideoEntity> dataList, RecyclerView recycler, boolean followLargeItemOnOddData, boolean isLiveChannels) {
this.dataList = dataList;
this.activity = fragmentActivity;
this.followLargeItemOnDataRender = followLargeItemOnOddData;
this.recyclerView = recycler;
isLiveChannel = isLiveChannels;
Log.e("VideoGridRVDataAdapter", "True");
if (this.recyclerView.getLayoutManager() instanceof GridLayoutManager) {
final GridLayoutManager gridLayoutManager = (GridLayoutManager) this.recyclerView.getLayoutManager();
recyclerciewScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) //check for scroll down
{
visibleItemCount = gridLayoutManager.getChildCount();
totalItemCount = gridLayoutManager.getItemCount();
pastVisiblesItems = gridLayoutManager.findFirstVisibleItemPosition();
if (!loading) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
loading = true;
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore(visibleItemCount + pastVisiblesItems);
}
}
}
}
}
};
this.recyclerView.addOnScrollListener(recyclerciewScrollListener);
}
}
public void notifyDataLoadingStartProgress() {
dataList.add(null);
notifyItemInserted(dataList.size() - 1);
}
public void notifyDataLoaded() {
dataList.remove(dataList.size() - 1);
notifyItemRemoved(dataList.size());
setLoaded();
}
public void resetItems(@NonNull List<VideoEntity> newDataSet) {
loading = false;
pastVisiblesItems = visibleItemCount = totalItemCount = 0;
dataList.clear();
addItems(newDataSet);
}
public void addItems(@NonNull List<VideoEntity> newDataSetItems) {
dataList.addAll(newDataSetItems);
postAndNotifyAdapter(new Handler());
}
public void addItem(VideoEntity item) {
if (!dataList.contains(item)) {
dataList.add(item);
notifyItemInserted(dataList.size() - 1);
}
}
public void removeItem(VideoEntity item) {
int indexOfItem = dataList.indexOf(item);
if (indexOfItem != -1) {
this.dataList.remove(indexOfItem);
notifyItemRemoved(indexOfItem);
}
}
private void postAndNotifyAdapter(final Handler handler) {
handler.post(new Runnable() {
@Override
public void run() {
if (!recyclerView.isComputingLayout()) {
try {
recyclerView.getRecycledViewPool().clear();
notifyDataSetChanged();
} catch (IndexOutOfBoundsException ex) {
}
} else {
postAndNotifyAdapter(handler);
}
}
});
}
public void destroy() {
this.recyclerView.removeOnScrollListener(recyclerciewScrollListener);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder vh;
View v;
if (viewType == VIEW_PROG) {
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_progressbar, viewGroup, false);
vh = new ProgressViewHolder(v);
} else {
Integer layoutResourceId;
if (isLiveChannel) {
layoutResourceId = R.layout.item_video_grid_norma_hs_overfow_overdraw;
} else {
layoutResourceId = R.layout.item_video_linear_overdraw;
}
v = LayoutInflater.from(viewGroup.getContext()).inflate(layoutResourceId, viewGroup, false);
vh = new MoviesItemRowHolder(this, dataList, v, activity);
}
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int i) {
if (holder instanceof MoviesItemRowHolder) {
VideoEntity singleItem = dataList.get(i);
((MoviesItemRowHolder) holder).bind(singleItem,dataList);
//setScaleAnimation(holder.itemView);
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
public boolean isLoading() {
return loading;
}
public void setLoaded() {
loading = false;
}
private void setFadeAnimation(View view) {
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(FADE_DURATION);
view.startAnimation(anim);
}
private void setScaleAnimation(View view) {
ScaleAnimation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setDuration(FADE_DURATION);
view.startAnimation(anim);
}
/**
* Return the view type of the item at <code>position</code> for the purposes
* of view recycling.
* <p/>
* <p>The default implementation of this method returns 0, making the assumption of
* a single view type for the adapter. Unlike ListView adapters, types need not
* be contiguous. Consider using id resources to uniquely identify item view types.
*
* @param position position to query
* @return integer value identifying the type of the view needed to represent the item at
* <code>position</code>. Type codes need not be contiguous.
*/
@Override
public int getItemViewType(int position) {
//return position;
return dataList.get(position) != null ? position : VIEW_PROG;
}
@Override
public int getItemCount() {
return (null != dataList ? dataList.size() : 0);
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
public List<VideoEntity> getItems() {
return dataList;
}
}
请看看我的类(class),让我知道出了什么问题,当我快速滚动时,这个崩溃发生在更少的设备上。
Stack Trace 是:
Fatal Exception: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 17(offset:17).state:18 android.support.v7.widget.RecyclerView{4266ee78 VFED.... .F....ID 0,0-480,625 #7f0902db app:id/recycler_view_list}, adapter:com.tapmad.tapmadtv.f.e@425896c0, layout:com.tapmad.tapmadtv.WrapContentLinearLayoutManager@424092b0, context:com.tapmad.tapmadtv.activities.SectionMoreActivityHS@4244c478
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline + 5923(RecyclerView.java:5923)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition + 5858(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition + 5854(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next + 2230(LinearLayoutManager.java:2230)
at android.support.v7.widget.GridLayoutManager.layoutChunk + 557(GridLayoutManager.java:557)
at android.support.v7.widget.LinearLayoutManager.fill + 1517(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.scrollBy + 1331(LinearLayoutManager.java:1331)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy + 1075(LinearLayoutManager.java:1075)
at android.support.v7.widget.GridLayoutManager.scrollVerticallyBy + 382(GridLayoutManager.java:382)
at android.support.v7.widget.RecyclerView.scrollStep + 1832(RecyclerView.java:1832)
at android.support.v7.widget.RecyclerView$ViewFlinger.run + 5067(RecyclerView.java:5067)
at android.view.Choreographer$CallbackRecord.run + 791(Choreographer.java:791)
at android.view.Choreographer.doCallbacks + 591(Choreographer.java:591)
at android.view.Choreographer.doFrame + 560(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run + 777(Choreographer.java:777)
at android.os.Handler.handleCallback + 725(Handler.java:725)
at android.os.Handler.dispatchMessage + 92(Handler.java:92)
at android.os.Looper.loop + 176(Looper.java:176)
at android.app.ActivityThread.main + 5317(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke + 511(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run + 1102(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main + 869(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(NativeStart.java)
非常感谢任何潜在客户。
最佳答案
我在你的代码中发现了问题
问题是当你清除arrayList时
dataList.clear();
您必须通知适配器数据集已更改。 else adapter不知道数据集被清空,计算出错
dataList.clear();
notifyDataSetChanged();
因此,recyclerview 适配器会知道发生了数据清除,并且会重新计算位置。当您添加新数据时,不会有任何不一致。
关于java - RecyclerView : Inconsistency detected. 项目位置无效 - 为什么会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58059064/
我正在使用 xamarin 表单开发移动应用程序,是否可以检测 Android 中的滑动手势以进行屏幕截图或单击锁定屏幕或主屏幕? 如果可以通过 Objective C/swift/java 实现,那
我刚刚涉足计算机视觉领域,并试图揭开它的各种复杂性的神秘面纱。我正在尝试使用冲浪特征检测器来增强卡尔曼滤波器。但是我不明白在使用冲浪特征在检测到的帧上构造单应性和有界矩形后如何调用和使用卡尔曼方法。在
问题 我正在尝试使用 opencv2 来检测静止图像中的 PlayStation Move 运动 Controller 。为了增加球体和背景之间的对比度,我决定修改输入图像以自动缩放每个 channe
我正在制作一个 android 应用程序,它可以从从视频中捕获的图像帧中检测对象。 openCV中的示例应用只有实时检测的例子。 附加信息:-我正在使用 Haar 分类器 截至目前,我正在将捕获的帧存
我在我的测试应用程序中成功实现了 OpenCV 平方检测示例,但现在需要过滤输出,因为它非常困惑 - 还是我的代码错误? 我对论文的四个角点感兴趣,以减少偏斜(如 that)和进一步处理......
在我的应用程序中,我想对 UIImage 使用人脸检测,所以我使用库中的 CoreImage 构建(我知道 Vision Library 也是最新的人脸检测库。但它仅支持 ios 11.0 及以上)。
我的 Linux 3.0/glibc 2.13 应用程序因以下形式的错误而停止: *** glibc detected *** MYAPP: double free or corruption (fa
我正在尝试运行一个基本程序来检测用户是否安装了 window.ethereum。当我运行我的程序时,我得到“@metamask/detect-provider:无法检测到 window.ethereu
我正在使用 Haarcascades 检测人脸和眼睛。我的问题是,它像眼睛一样包围了许多盒子。我的语法是 face_cascade = cv2.CascadeClassifier('haarcasca
我如何检测用户在对话中点击“消息”? 如果 MessageViewController Controller 很紧凑并且用户向上滑动我如何检测到它? 我试过这些代表,但它不能正常工作 override
我在对象检测中使用 Microsoft 自定义视觉服务来提取需要的对象。我想做一个回归测试来比较结果。但是,我找不到一个地方可以导出带有用户通过 GUI 定义的边界框的训练图片。 模型训练是在 Mic
我对 chrome 进行了扩展。当我浏览到 https 下的网站时,我的链接仍在 http 下,我收到:“该站点使用 SSL,但 Google Chrome 检测到页面上存在高风险不安全内容或站点证书
我用的是JetBrains的Gogland工具来学习go语言,我安装成功了,但是打不开,现将报错列如下,有没有人遇到过这个问题?如能解决,将不胜感激。 错误: Java 运行时环境检测到 fatal
我想在 R 中评估和比较我的社区检测算法的结果。我的算法不允许重叠,并且有一些节点没有被处理。例如,对于 Zachary 空手道俱乐部,我有 1 个节点未处理。我找到了很多指标(NMI、ARI、Mod
是否有任何好的开源引擎来检测文本使用的语言,也许是概率度量?我可以在本地运行并且不查询 Google 或 Bing 的一个?我想在大约 1500 万页的 OCR 文本中检测每个页面的语言。 并非所有文
我正在开发一款 2D 游戏,其中包含高速射弹,这些射弹会撞击高速(并且可能旋转得非常快)的多边形目标对象。我一直在试验和寻找适合我的强大碰撞检测算法。 如果快速旋转不是一个因素(即 0 或慢速旋转),
我正在制作一款平台游戏,其中有玩家和一些 Prop 。为了检查碰撞,我使用了 matterjs collisionactive 函数: this.matter.world.on("collisiona
我已经习惯于使用矩形进行碰撞检测,现在我有点难住了。我正在处理类似菱形的形状,在过去的几个小时里,我一直在试图找出如何检查碰撞。 我尝试检查第一个对象的四个点是否在第二个对象的点内,但这只是一个框(我
最初,两个半径为 R1 和 R2 的非接触球体处于静止状态。 然后在时间 = 0 时分别给它们两个加速度 a1 和 a2。查明他们是否会接触。它们的初始位置分别表示为 (x1,y1,z1) 和 (x2
我目前正在学习使用 LWJGL 和 OpenGL 的 ThinMatrix 3d 游戏开发教程。我正在尝试在我的游戏中实现碰撞检测。我已经准备好检测 AABB 与 AABB 碰撞的代码,但似乎无法弄清
我是一名优秀的程序员,十分优秀!