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

"Page must be 0 when pageCount is 0" thrown when pageCount > 0 #859

Closed
zoltish opened this issue Nov 5, 2021 · 16 comments
Closed

"Page must be 0 when pageCount is 0" thrown when pageCount > 0 #859

zoltish opened this issue Nov 5, 2021 · 16 comments
Assignees

Comments

@zoltish
Copy link

zoltish commented Nov 5, 2021

Describe the bug

Scrolling through a HorizontalPager throws this exception in rare cases, Ive seen 3 crash logs in a group of about 1000 users. The pager always shows a fixed list that never changes. I believe the issue is triggered by recompositions happening while scrolling the pager.

Environment:

  • Android OS version: [Android 7 & 9]
  • Device: [Emulated Nexus 5 & Huawei P10]
  • Accompanist version: [0.21.1-beta]
@cristianbtg
Copy link

I saw this happening starting v0.19.0 when a configuration change happened (e.g. language) where during recomposition the pageCount and currentPage were set to 0, even though I was using the rememberPagerState().

Before v0.19.0 PagerState.Saver was saving pageCount and currentPage and it was working fine; currently it's only saving pageCount, but from my observation, during the first recomposition it's 0 so it might lead to some crashes.

@aazizbekyan
Copy link

The workaround for time being:

    LaunchedEffect(pagerState.pageCount) {
        if (pagerState.pageCount != 0) {
             // now interact with pagerState
        }
    }

@andkulikov andkulikov self-assigned this Dec 3, 2021
@andkulikov
Copy link
Collaborator

Can you show the crash stacktrace?

@zoltish
Copy link
Author

zoltish commented Dec 3, 2021

Sorry, Ive switched crash reporting tools since my post and I no longer have access to it. I guess the good news is that I havent seen it since then in a group of 5000 users. Ill let you know if it pops up again!

@yschimke
Copy link
Contributor

I've hit a tangential bug.

I can define initialPage to either of my two pages.

    val pagerState = rememberPagerState(initialPage = 0)

If in a launched effect I want to update the initial page or scroll to a page, then I can only do that once the composable has rendered. Which would be the scrollToPage case.

This means I can update the initial page once by launched effect completes.

@yschimke
Copy link
Contributor

Never mind, I think I can just move my LaunchedEffect inside to where I have already created the HorizontalPager.

@yasanglass
Copy link

This is the most common crash on my Compose app, I just started handling it by just checking the page count.

@andkulikov I use Firebase Crashlytics so I do have the crash log:

Fatal Exception: java.lang.IllegalArgumentException: page must be 0 when pageCount is 0
at com.google.accompanist.pager.PagerState.requireCurrentPage(PagerState.java:1)
at com.google.accompanist.pager.PagerState.animateScrollToPage$default(PagerState.java:26)
at yasan.space.mnml.ai.launcher.ui.main.MainActivity$HandleGlobalEvents$1$1.invokeSuspend(MainActivity.java:26)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:2)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:86)
at androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch(AndroidUiDispatcher.java:1)
at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run(AndroidUiDispatcher.java:2)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7864)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)

arch_disk_io_0
at sun.misc.Unsafe.park(Unsafe.java)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:190)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2067)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1092)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)

pool-4-thread-1
at sun.misc.Unsafe.park(Unsafe.java)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:190)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2067)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1120)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:849)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1092)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)

pool-14-thread-1
at sun.misc.Unsafe.park(Unsafe.java)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:190)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2067)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1092)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)

I can send the rest if you want as well.

@dbobrzyk
Copy link

dbobrzyk commented Jan 21, 2022

@andkulikov PagerState also crashes on version accompanist = "0.17.0"

Fatal Exception: java.lang.IllegalArgumentException
page must be 0 when pageCount is 0
com.google.accompanist.pager.PagerState.requireCurrentPage (PagerState.kt:656)
com.google.accompanist.pager.PagerState.updateLayoutPages (PagerState.kt:486)
com.google.accompanist.pager.PagerState.setPageCount (PagerState.java:221)
com.google.accompanist.pager.PagerStateKt.rememberPagerState (PagerState.kt:81)

@andkulikov
Copy link
Collaborator

andkulikov commented Jan 21, 2022

@dbobrzyk

Please try to upgrade to the latest version before reporting any bugs - https://github.com/google/accompanist/releases

@3laeddine-dh
Copy link

I cant Solve it . Any Solution !!

@yasanglass
Copy link

yasanglass commented Feb 13, 2022

I cant Solve it . Any Solution !!

@3laeddine-dh Just check the page size before changing the current page:

if (pagerState.pageCount != 0) pagerState.animateScrollToPage(MyPages.TargetPage)

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the stale Stale issues which are marked for closure label Mar 16, 2022
@dbobrzyk
Copy link

dbobrzyk commented Mar 21, 2022

@dbobrzyk

Please try to upgrade to the latest version before reporting any bugs - https://github.com/google/accompanist/releases

After updating to version 0.23.0 issue is still present.

@andkulikov andkulikov removed the stale Stale issues which are marked for closure label Mar 21, 2022
@wakaztahir
Copy link

wakaztahir commented Apr 10, 2022

I am using latest version v0.24.6-alpha , It crashes when I try to scroll to page , even when the content has been rendered completely , and in fact , the page count is always 0
So I have a button and I press it and it crashes right away !

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.z7apps.familytracker, PID: 18730
    java.lang.IllegalArgumentException: page must be 0 when pageCount is 0
        at com.google.accompanist.pager.PagerState.requireCurrentPage(PagerState.kt:328)
        at com.google.accompanist.pager.PagerState.animateScrollToPage(PagerState.kt:201)
        at com.google.accompanist.pager.PagerState.animateScrollToPage$default(PagerState.kt:197)
        at com.z7apps.familytracker.ui.setup.SetupScreenKt$PermissionsSetup$1$2$1.invokeSuspend(SetupScreen.kt:88)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch(AndroidUiDispatcher.android.kt:81)
        at androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch(AndroidUiDispatcher.android.kt:41)
        at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run(AndroidUiDispatcher.android.kt:57)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:240)
        at android.app.ActivityThread.main(ActivityThread.java:7829)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:512)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1027)

@arbyruns
Copy link

arbyruns commented Feb 8, 2023

I was able to solve this by adding state = pagerState to HorizontalPager. So something like this

val pagerState = rememberPagerState()

HorizontalPager(count = 10, state = pagerState) { page ->
...
}

@undermark5
Copy link

undermark5 commented Feb 13, 2023

@arbyruns This was fixed by #1195, which is included as of https://github.com/google/accompanist/releases/tag/v0.24.11-rc. If you can update the library, to that version or newer, this is no longer an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests