Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexOutOfBoundsException but I can't reproduce it. #178

Closed
MRDHR opened this issue Jan 8, 2024 · 5 comments
Closed

IndexOutOfBoundsException but I can't reproduce it. #178

MRDHR opened this issue Jan 8, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@MRDHR
Copy link

MRDHR commented Jan 8, 2024

Our app used dpadrecycliew and also encountered IndexOutOfBoundsException,

Using 1.2.0-alpha02.

The following is the stack information when an error occurs

androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6783) androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6757) androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6753) com.rubensousa.dpadrecyclerview.layoutmanager.layout.linear.LinearLayoutEngineer.initLayout(LinearLayoutEngineer.kt:63) com.rubensousa.dpadrecyclerview.layoutmanager.layout.StructureEngineer.layoutChildren(StructureEngineer.kt:205) com.rubensousa.dpadrecyclerview.layoutmanager.layout.PivotLayout.layoutChildren(PivotLayout.kt:124) com.rubensousa.dpadrecyclerview.layoutmanager.layout.PivotLayout.onLayoutChildren(PivotLayout.kt:89) com.rubensousa.dpadrecyclerview.layoutmanager.PivotLayoutManager.onLayoutChildren(PivotLayoutManager.kt:128) androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4645) androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4348) androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4919) android.view.View.layout(View.java:22501) android.view.ViewGroup.layout(ViewGroup.java:6528) android.widget.FrameLayout.layoutChildren(FrameLayout.java:334) android.widget.FrameLayout.onLayout(FrameLayout.java:270) android.view.View.layout(View.java:22501) android.view.ViewGroup.layout(ViewGroup.java:6528) androidx.appcompat.widget.LinearLayoutCompat.setChildFrame(LinearLayoutCompat.java:1669) androidx.appcompat.widget.LinearLayoutCompat.layoutHorizontal(LinearLayoutCompat.java:1658) androidx.appcompat.widget.LinearLayoutCompat.onLayout(LinearLayoutCompat.java:1433) android.view.View.layout(View.java:22501) android.view.ViewGroup.layout(ViewGroup.java:6528) android.widget.FrameLayout.layoutChildren(FrameLayout.java:334) android.widget.FrameLayout.onLayout(FrameLayout.java:270) android.view.View.layout(View.java:22501) android.view.ViewGroup.layout(ViewGroup.java:6528) android.widget.FrameLayout.layoutChildren(FrameLayout.java:334) android.widget.FrameLayout.onLayout(FrameLayout.java:270) android.view.View.layout(View.java:22501) android.view.ViewGroup.layout(ViewGroup.java:6528) razerdp.basepopup.PopupDecorViewProxy.layoutInternal(PopupDecorViewProxy.java:597) razerdp.basepopup.PopupDecorViewProxy.onLayout(PopupDecorViewProxy.java:378) android.view.View.layout(View.java:22501) android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3276) android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2235) android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9043) android.view.Choreographer$CallbackRecord.run(Choreographer.java:1280) android.view.Choreographer.doCallbacks(Choreographer.java:1019) android.view.Choreographer.doFrame(Choreographer.java:911) android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1248) android.os.Handler.handleCallback(Handler.java:900) android.os.Handler.dispatchMessage(Handler.java:103) android.os.Looper.loop(Looper.java:219) android.app.ActivityThread.main(ActivityThread.java:8673) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)

@rubensousa
Copy link
Owner

rubensousa commented Jan 9, 2024

Hi @MRDHR can you please share the full stacktrace? I'm interested in knowing if the index was greater than the adapter size or if it was negative.

@rubensousa
Copy link
Owner

Hey @MRDHR. I managed to reproduce the crash with this UI test:

    @Test
    fun testSelectingPositionOutOfBoundsDoesNotCrash() {
        val numberOfItems = 500
        launchFragment(TestAdapterConfiguration(numberOfItems = numberOfItems))

        selectPosition(numberOfItems + 100, smooth = true, waitForIdle = false)

        Thread.sleep(1000L)

        mutateAdapter { adapter ->
            adapter.setList(MutableList(numberOfItems + 10) { it })
            adapter.notifyItemRangeInserted(0, 100)
        }

        onRecyclerView("Request layout") { recyclerView ->
            recyclerView.requestLayout()
        }

        assertFocusAndSelection(numberOfItems + 9)
    }

Please check if you're doing the following in your side:

  1. Using DiffUtil and submitList to notify adapter changes correctly
  2. Check if your setSelectedPosition calls are using indexes within bounds of the adapter contents

There's a PR here to address this specific crash now, but you might still hit some issues if you don't follow those 2 steps: #179

@MRDHR
Copy link
Author

MRDHR commented Jan 10, 2024

Regarding the two points you mentioned

1: I did not use DiffUtil to update data, I used replace. That is, items clear, then add all, and finally execute notifydatasetchanged
2: I can ensure that my items are not empty with setSelectedPosition.

The data updates are not very frequent, and they are not updated every 1 second.

At present, I have added the setSelectedPosition operation to be executed after recyclerview.post, and added the judgment for position>=0&&position<=items. size -1

@MRDHR
Copy link
Author

MRDHR commented Jan 10, 2024

Looking forward to your pull request being synchronized to the master and release an aar

@rubensousa
Copy link
Owner

@MRDHR released in 1.2.0-alpha03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants