如何停止 Android recyclerview 中搜索过滤器的不断崩溃

How to stop Incessant crash on search filter in Android recyclerview

每次我在搜索视图中进行搜索时,我都在努力阻止索引越界异常崩溃。我尝试了网上列出的几件事: 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

但到目前为止没有任何帮助。这个问题是我的代码特有的,只是想不通是什么问题。

这是我的崩溃报告:

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

和我的 activity 代码: 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 或其他一些数字,通常发生在我在搜索视图中键入并且过滤器搜索匹配字符时)

很高兴分享更多代码或在必要时详细解释。已经为此苦苦挣扎了好几天,但没有运气:(

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

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