当前位置: 首页 > 知识库问答 >
问题:

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

司空奕
2023-03-14

每次在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(偏移量:0)”崩溃(有时是16或其他数字,通常发生在我在searchview中键入时,过滤器通过匹配的字符进行搜索)

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

共有1个答案

锺离俊雄
2023-03-14

Filter实现存在问题。您正在更新performFilding(CharSequence charSequence)方法中的mSearchGuestList响应列表过滤,这是错误的来源。您必须在Publish结果(CharSequence charSequence, Filter结果过滤器结果)方法中更新您的列表。所以你必须从你的适配器代码中删除mSearchGuestList应列表过滤=filteredList;(第234行)。此外,你在getItemId()onBindViewHolder()方法中调用了通知ItemChanged()方法。我认为这也是错误的实现,你应该删除它们。

 类似资料:
  • 问题内容: 每当我在searchview中进行搜索时,我一直在努力阻止索引超出范围异常崩溃。我尝试了网上列出的几件事: 在搜索中使数组脱离索引绑定异常 https://www.codeproject.com/Questions/1040119/Android-app-is-crashing-with- Index-out-of-range-is 但到目前为止没有任何帮助。这个问题是我的代码特有的,

  • 问题内容: 我是这个框架的新手,因此练习Angularjs并遵循网站上提供的教程。 有一个示例,我们可以搜索表中存在的数据,示例如下, 在上面的代码中,我可以使用两个不同的输入来搜索手机,即按型号名称搜索和按公司名称搜索,以上代码运行良好, 但是,如果我需要使用选择选项中存在的搜索类型进行搜索,该怎么办? 代码如下 从上面的代码中,您可以看到我正在尝试通过选择框中显示的“姓名”,“公司”或“名称”

  • 每次启动Farming Valley服务器时,都会出现以下错误: 我基本上没有这方面的经验,我不明白这是怎么回事。如有任何帮助,我们将不胜感激!提前道谢。知道我从OVH那里买了一台服务器可能会很好

  • 问题内容: 我正在尝试搜索数据库,并能够使用大写/小写过滤器术语,但是我注意到在应用分析器时,我不知道如何在过滤后的搜索中应用小写分析器。这是查询: 我有一个小写的类型: 以及相应的映射: 问题答案: 问题是您在索引期间分析了一个字段以使其小写,但您正在对未分析的查询使用术语过滤器: 术语过滤器 筛选具有包含术语(未分析)的字段的文档。与术语查询类似,不同之处在于它充当过滤器。 http://ww

  • 问题内容: 有一个Spring Boot WebMVC应用程序,还有一个从AbstractPreAuthenticatedProcessingFilter继承的bean,我将其显式添加到Spring Security过滤器链中的特定位置。我的Spring Security配置如下所示: 安全配置有效。问题是,因为PreAuthenticationFilter类继承自AbstractPreAuthe

  • 我正在使用以下搜索: 我现在想使用弹性搜索在索引过程中提供的id ()来过滤结果。例如,{}。我猜你得用查询这个词。结果应该是只有当< code>_id匹配时,文档才返回。我该怎么做呢?