androidandroid-viewpagerandroid-jetpack-compose

Jetpack Compose HorizontalPager exception


I got crash when I used pager in my app

I used a HorizontalPager from androidx.compose.foundation.pager in my app. My function's @Preview work properly even in interaction mode, but when I run the app I got this exception:

java.lang.NoSuchMethodError: No static method HorizontalPager-AlbwjTQ(ILandroidx/compose/ui/Modifier;Landroidx/compose/foundation/pager/PagerState;Landroidx/compose/foundation/layout/PaddingValues;Landroidx/compose/foundation/pager/PageSize;IFLandroidx/compose/ui/Alignment$Vertical;Landroidx/compose/foundation/gestures/snapping/SnapFlingBehavior;ZZLkotlin/jvm/functions/Function1;Landroidx/compose/ui/input/nestedscroll/NestedScrollConnection;Lkotlin/jvm/functions/Function3;Landroidx/compose/runtime/Composer;III)V in class Landroidx/compose/foundation/pager/PagerKt; or its super classes (declaration of 'androidx.compose.foundation.pager.PagerKt' appears in /data/app/~~KGVF3sYZNEP19lh8ZxkKbQ==/packagename-Uy9s9dyOAJmH_cCZaHyTLw==/base.apk)

My code:

val pages = lost.groupBy { it.pageNO }

    HorizontalPager(pageCount = pages.size, beyondBoundsPageCount = 3) { index ->
        SomeComposeView(
            list = pages.get(index.plus(1))!!
        )
    }

My compose dependencies:

compose-bom = "2023.06.01"
compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "compose-bom" }
compose-ui = { group = "androidx.compose.ui", name = "ui" }
compose-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
compose-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
compose-foundation = { group = "androidx.compose.foundation", name = "foundation" }
compose-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout" }
compose-runtime = { group = "androidx.compose.runtime", name = "runtime" }
compose-ui-util = { group = "androidx.compose.ui", name = "ui-util" }
material-icons-extended = { group = "androidx.compose.material", name = "material-icons-extended" }
ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
material-icons-core = { group = "androidx.compose.material", name = "material-icons-core" }
compose-material3 = { group = "androidx.compose.material3", name = "material3" }```


Solution

  • After 3 days I found the problem. The problem that caused the app crash was I didn't add the accompanist pager dependency. Yes, We need both androidx.compose.foundation and accompanist-pager dependencies to use Pager views.