小编典典

如何在Android recyclerview中停止搜索过滤器上的持续崩溃

java

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

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

和我的活动代码: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或其他数字,通常发生在我输入searchview,然后过滤器搜索匹配的字符)

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


阅读 257

收藏
2020-11-30

共1个答案

小编典典

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

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

2020-11-30