简体   繁体   English

滚动滚动获取RecyclerView异常:-“ java.lang.IndexOutOfBoundsException:检测到不一致。 无效的商品位置21”

[英]On scroll the RecyclerView getting exception : -“ java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 21”

I am using the RecyclerView with Sqlite in my app. 我在我的应用程序中将RecyclerViewSqlite一起使用。 As i have load the RecyclerView from the SQlite and than after call service in background and try to notify the RecyclerView by notifyDataSetChanged() method but getting the unexpected exception while scrolling the RecyclerView ( when RecyclerView is updating or notifying). 因为我已经从SQlite加载了RecyclerView ,并且比在后台调用服务之后加载了尝试,并尝试通过notifyDataSetChanged()方法通知RecyclerView ,但是在滚动RecyclerView时遇到意外异常(当RecyclerView正在更新或通知时)。

Please check my setAdapter() which i am using to notify and set the adapter on RecyclerView 请检查我用于通知的setAdapter()并在RecyclerView上设置适配器

  private void setAdapter() {

        if (adapter == null) {
            adapter = new EmojiAdapter(stickerArrayList, getActivity(), "Sticker", stickerIdArrayList);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
            rvEmoji.setLayoutManager(mLayoutManager);
            rvEmoji.setItemAnimator(new DefaultItemAnimator());
            rvEmoji.setAdapter(adapter);
        } else {

            adapter.notifyDataSetChanged();

        }

    }

I have tried all RecyclerView.stopScroll() , RecyclerView.getRecycledViewPool().clear() but problem is not short out 我已经尝试了所有RecyclerView.stopScroll()RecyclerView.getRecycledViewPool().clear()但问题并不出局

Exception 例外

java.lang.IndexOutOfBoundsException: Inconsistency detected. java.lang.IndexOutOfBoundsException:检测到不一致。 Invalid item position 21(offset:21).state:32 at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5546) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5482) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5478) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2215) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1542) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1502) at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1316) at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1061) at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4769) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallb android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView)上无效的项目位置21(offset:21).state:32 at android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5546) .java:5482),位于android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5478),位于android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2215)。 android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1502)上的support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1542)android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager。 android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1061)at android.support.v7.widget.RecyclerView $ ViewFlinger.run(RecyclerView.java:4769)at android.view.Choreographer android.view.Choreographer.doCallb上的$ CallbackRecord.run(Choreographer.java:761) acks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:543) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5095) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) at dalvik.system.NativeStart.main(Native Method) android.view.Choreographer.doFrame(Choreographer.java:543 :)上的acks(Choreographer.java:574)android.os.Handler.handleCallback(Handler)上的android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)上的acks(Choreographer.java:543) .java:733),位于android.os.Handler.dispatchMessage(Handler.java:95),位于android.os.Looper.loop(Looper.java:136),位于android.app.ActivityThread.main(ActivityThread.java:5095)在java.lang.reflect.Method.invokeNative(本机方法)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:的java.lang.reflect.Method.invoke(Method.java:515)处786)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)在dalvik.system.NativeStart.main(本机方法)

There are alots of simliar question but i am not getting the expected result.Here is the link which i have visited before posting the question here. 有很多类似的问题,但我没有得到预期的结果。这是我在此处发布问题之前访问过的链接。
1. First link 1.第一个链接
2. Second link 2.第二个链接
3. Third link 3.第三个环节
4. Forth link 4.第四链接
5. Fifth link 5.第五环节
6. Sixth link 6.第六环节

I have visited all the link and tried their best solutions , but it is not working for me.. 我已经访问了所有链接并尝试了他们的最佳解决方案,但是它对我不起作用。

I am using the following gradle version for the RecyclerView 我正在为RecyclerView使用以下gradle版本

compile 'com.android.support:recyclerview-v7:26.+'

Please check the Adapter class which i am using as follow:- 请检查我正在使用的Adapter类,如下所示:

public class EmojiAdapter extends RecyclerView.Adapter<EmojiAdapter.MyViewHolder> {

    private Activity activity;
    private HashMap<String, ArrayList<EmojiBean>> arrayList = new HashMap<>();
    private String sticker_emoji;
    private ArrayList<String> keyArrayList = new ArrayList<>();

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private RecyclerView rvItemCreation;
        private TextView txtStickerName, txtBy, txtCreator;
        private ImageView imgForward, imgDownload;

        private MyViewHolder(View view) {
            super(view);

            rvItemCreation = (RecyclerView) view.findViewById(R.id.rvItemCreation);
            txtStickerName = (TextView) view.findViewById(R.id.txtStickerName);
            txtBy = (TextView) view.findViewById(R.id.txtBy);
            txtCreator = (TextView) view.findViewById(R.id.txtCreator);
            imgForward = (ImageView) view.findViewById(R.id.imgForward);
            imgDownload = (ImageView) view.findViewById(R.id.imgDownload);

            txtStickerName.setTypeface(Constant.setCustomFont(activity, "Montserrat-SemiBold.otf"));
            txtBy.setTypeface(Constant.setCustomFont(activity, "Montserrat_Regular.otf"));
            txtCreator.setTypeface(Constant.setCustomFont(activity, "Montserrat_Regular.otf"));

        }
    }


    public EmojiAdapter(HashMap<String, ArrayList<EmojiBean>> arrayList, Activity activity, String sticker_emoji, ArrayList keyArrayList) {
        this.arrayList = arrayList;
        this.activity = activity;
        this.sticker_emoji = sticker_emoji;
        this.keyArrayList = keyArrayList;


    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_media_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {


        if (sticker_emoji.equalsIgnoreCase("Sticker")) {
            holder.imgForward.setImageResource(R.drawable.forward_icon);
        } else {
            holder.imgForward.setImageResource(R.drawable.delete_icon);
        }


        if (arrayList.size() > position) {
            MyCreationItemAdapter mAdapter = new MyCreationItemAdapter(arrayList.get(keyArrayList.get(position)), activity);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false);
            holder.rvItemCreation.setLayoutManager(mLayoutManager);
            holder.rvItemCreation.setItemAnimator(new DefaultItemAnimator());
            holder.rvItemCreation.setAdapter(mAdapter);
        }


    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }


}

Please help me to short out from the problem.Thanks 请帮助我解决问题。谢谢

Update 更新
What i have done to resolve this conflict that I have done disable user interface(Show some progress bar) while loading the data. 为了解决此冲突,我所做的事情是我在加载数据时禁用了用户界面(显示一些进度栏)。

I had the same question with "java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state". 我对“ java.lang.IndexOutOfBoundsException:检测到不一致。项目位置6(offset:6).state无效”存在相同的问题。 This crash is show with pullrefrash , when you had loadmore the list is more than you first loaded . 使用pullrefrash显示此崩溃,当您加载了更多信息时,该列表的数量便超过了首次加载的数量。 then you pulltorfrash new data and clear the old list data ,at this moment you adapter's data size is not more than you new data size. 然后您将新数据拖出并清除旧列表数据,此时适配器的数据大小不超过新数据大小。

Try commenting this line of code: 尝试注释以下代码行:

rvEmoji.setItemAnimator(new DefaultItemAnimator());

Check if it works now. 检查现在是否可以使用。 If it does then the error you faced is because the PredictiveAnimations are enabled. 如果确实如此,那么您面临的错误是因为启用了PredictiveAnimations。 To use your code with animation you'll have to subclass the specific layout manager that you are using and override the supportsPredictiveItemAnimations() method and return false. 要将代码与动画一起使用,您必须将正在使用的特定布局管理器子类化,并覆盖supportsPredictiveItemAnimations()方法并返回false。

@Override 
public boolean supportsPredictiveItemAnimations() { 
       return false; 
}

For detailed understanding have a look here . 有关详细的了解,请在这里查看

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecyclerView:检测到不一致。 无效项目 position - 为什么? - RecyclerView: Inconsistency detected. Invalid item position - why is that? 滚动上的 Recycleview 在最后一个项目位置抛出 java.lang.IndexOutOfBoundsException - Recycleview on scroll throws java.lang.IndexOutOfBoundsException on last item position 检测到不一致。 无效的项目位置0(偏移:-1)…删除第一个项目时,RecyclerView崩溃 - Inconsistency detected. Invalid item position 0(offset:-1)… RecyclerView crashes, on deleting first item RecyclerView:检测到不一致。 无效的项目位置。 原因 - 使用计时器删除项目 - RecyclerView: Inconsistency detected. Invalid item position. Cause -Removing item using timer RecyclerView和java.lang.IndexOutOfBoundsException视图持有者适配器positionViewHolder无效 - RecyclerView and java.lang.IndexOutOfBoundsException Invalid view holder adapter positionViewHolder java.lang.IndexOutOfBoundsException:无效的索引1,在Sectioned RecyclerView中大小为1 - java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 in Sectioned RecyclerView recyclerview onclicklistener java.lang.IndexOutOfBoundsException:无效的索引0,大小为0 - recyclerview onclicklistener java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 RecyclerView - java.lang.IndexOutOfBoundsException:索引 1 无效,大小为 0 - RecyclerView - java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 致命异常:java.lang.IndexOutOfBoundsException 无效索引 0,大小为 0 - Fatal Exception: java.lang.IndexOutOfBoundsException Invalid index 0, size is 0 java引发异常java.lang.IndexOutOfBoundsException: - java throwing exception java.lang.IndexOutOfBoundsException:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM