How to stop Incessant crash on search filter in Android recyclerview

∥☆過路亽.° 提交于 2020-02-09 17:20:29

问题


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.

Here's my crash report:

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)

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

and my activity code: https://pastebin.com/wJHT1q9H

and presenter code: https://pastebin.com/aKn0TFpz

here's my 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>

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 :(


回答1:


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.



来源:https://stackoverflow.com/questions/59885424/how-to-stop-incessant-crash-on-search-filter-in-android-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!