本文介绍了如何在Android recyclerview中停止搜索过滤器上的持续崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在searchview中进行搜索时,我一直在努力阻止索引超出范围异常崩溃.我尝试了网上列出的几件事:在搜索中使数组脱离索引绑定异常

I have been struggling to stop an index out of bounds exception crash every single time I do a search in a searchview. I tried a couple of things listed online :on search getting array out of index bound exception

https ://www.codeproject.com/Questions/1040119/Android-app-is-crashing-with-Index-out-of-range-is

但到目前为止没有任何帮助.这个问题是我的代码特有的,只是无法弄清楚是什么问题.

but nothing helped so far. The issue is something specific to my code, just can't figure out what's the issue.

这是我的崩溃报告:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test.dev, PID: 12873
    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:0).state:146 android.support.v7.widget.RecyclerView{3135a5 VFED..... ......ID 0,0-1440,66822 #7f09023b app:id/search_guests_recycler_view}, adapter:com.test.searchGuests.SearchGuestListAdapter@5ea4d7a, layout:android.support.v7.widget.LinearLayoutManager@eba592b, context:com.test.SearchGuestActivity@6eb13
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5923)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
        at android.view.View.measure(View.java:19857)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:19857)
        at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1293)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.widget.ScrollView.onMeasure(ScrollView.java:340)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:733)
        at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:805)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:733)
        at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:805)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
        at android.view.View.measure(View.java:19857)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
        at android.view.View.measure(View.java:19857)

这是我的适配器代码: https://pastebin.com/fd80ccwF

Here's my adapter code:https://pastebin.com/fd80ccwF

和我的活动代码: https://pastebin.com/wJHT1q9H

和演示者代码: https://pastebin.com/aKn0TFpz

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:clickable="false">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/cl_root_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false">

        <LinearLayout
            android:id="@+id/activity_new_guest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:elevation="0dp">


                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white"
                    android:paddingBottom="17dp"
                    app:subtitleTextColor="@color/white"
                    app:titleTextAppearance="@style/Toolbar.TitleText"
                    app:titleTextColor="@color/white">


                    <TextView
                        android:id="@+id/nav_back_btn"
                        style="@style/Body1RegLeftBlack"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/activity_horizontal_margin"
                        android:layout_marginTop="20dp"
                        android:background="@drawable/ic_baseline_close_24px"
                        android:gravity="center_horizontal" />

                    <TextView
                        android:id="@+id/search_guest_main_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="18dp"
                        android:layout_marginTop="20dp"
                        android:gravity="center_horizontal"
                        android:letterSpacing="0.01"
                        android:layout_gravity="center"
                        android:text="@string/past_guests"
                        android:textAllCaps="true"
                        android:textAppearance="@style/TextAppearance.Text.RidleyGrotesk.Medium"
                        android:textColor="#de000000"
                        android:textSize="18sp"
                        android:textStyle="normal"

                        />

                </android.support.v7.widget.Toolbar>


            </android.support.design.widget.AppBarLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="24dp"
                    android:textColor="#a5a6b8"
                    android:textAppearance="@style/TextAppearance.Text.Graphik.Semibold"
                    android:textSize="14sp"
                    android:textAllCaps="true"
                    android:layout_marginTop="7dp"
                    android:text="@string/search_past_guests"/>
                <SearchView
                    android:layout_width="match_parent"
                    android:layout_height="52dp"
                    android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                    android:textSize="16sp"
                    android:id="@+id/search_guest_list"
                    android:textColor="@color/black"
                    android:background="#f6f6f6"
                    android:layout_marginEnd="24dp"
                    android:layout_marginStart="24dp"
                    android:layout_marginTop="7dp"
                    android:singleLine="true"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:hint="@string/search_past_guests"/>
            </LinearLayout>
            <ScrollView
                android:id="@+id/info_scrollview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
               >

               <RelativeLayout
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content">
                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/search_guests_recycler_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="65dp"
                        tools:listitem="@layout/item_past_guest" />
                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/no_match_relativelayout"
                        android:visibility="gone">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="No match found"
                            android:layout_marginStart="24dp"
                            android:layout_marginEnd="24dp"
                            android:singleLine="true"
                            android:layout_marginTop="18dp"
                            android:layout_marginBottom="18dp"
                            android:textAppearance="@style/TextAppearance.Text.Roboto.Regular"
                            android:textSize="16sp"
                            android:ellipsize="end"
                            android:textColor="@color/black"
                            android:id="@+id/no_match_textview"
                            />
                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="@color/registerUserSeparatorColor"
                            android:layout_below="@id/no_match_textview"
                            android:layout_marginStart="16dp"
                            android:layout_marginEnd="16dp"/>
                    </RelativeLayout>
               </RelativeLayout>

            </ScrollView>
        </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>
</android.support.design.widget.CoordinatorLayout>

任何想法我可能会丢失或未正确执行的操作,始终会崩溃,并显示:java.lang.IndexOutOfBoundsException:检测到不一致.项目位置为0(offset:0)无效."(有时为16或其他一些数字,通常会在我输入搜索视图并且过滤器搜索匹配的字符时发生)

Any idea what I could be missing or not doing correctly, that it always crashes with ":java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:0)."(sometimes 16 or some other number, usually happens when i type in the searchview and the filter searches through the matching characters)

乐于分享更多代码或在必要时进行详细解释.已经为此苦苦挣扎了好几天,但没有运气:(

Happy to share more code or explain in detail if necessary. Have been struggling with this for days ,but no luck :(

推荐答案

Filter实现存在问题.您正在使用performFiltering(CharSequence charSequence)方法更新mSearchGuestListResponseListFiltered,它是错误的根源.您必须使用publishResults(CharSequence charSequence, FilterResults filterResults)方法更新列表.因此,您必须从适配器代码中删除mSearchGuestListResponseListFiltered = filteredList;(第234行).

此外,您在getItemId()onBindViewHolder()方法中调用了notifyItemChanged()方法.我认为这也是错误的实现,应该删除它们.

There is a problem with Filter implementation. You are updating mSearchGuestListResponseListFiltered in the performFiltering(CharSequence charSequence) method and it's source of the error. You must update your list in publishResults(CharSequence charSequence, FilterResults filterResults) method. So you must remove mSearchGuestListResponseListFiltered = filteredList; (Line 234) from your adapter code.

In addition, you called notifyItemChanged() method in getItemId() and onBindViewHolder() methods. I think it's wrong implementation as well and you should remove them.

这篇关于如何在Android recyclerview中停止搜索过滤器上的持续崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 05:12