Jetpack Compose中AnimatedNavHost抛出java.lang.ClassCastException异常求助
I’ve hit this exact compatibility snag before, so let’s walk through how to resolve it quickly.
The Root Cause
Your accompanist-navigation-animation:0.16.1 version is outdated and clashes with your Kotlin (1.5.21) and Compose Compiler (1.0.2) setup. This old Accompanist version relies on an older Compose Navigation library that uses different internal collection types—triggering that frustrating ArrayList → Set cast error. The standard NavHost works because it’s part of the official AndroidX Compose Navigation artifact that’s aligned with your current dependencies.
Step-by-Step Fix
Upgrade your Accompanist Navigation Animation dependency
Swap your existing Accompanist dependency for a version compatible with your Kotlin/Compose Compiler combo. For Kotlin 1.5.21 and Compose Compiler 1.0.2,0.24.10-rcis a reliable pick:implementation "com.google.accompanist:accompanist-navigation-animation:0.24.10-rc"Align Compose dependencies (optional but recommended)
If you’re not using the Compose BOM to manage versions, double-check your core Compose dependencies match the version expected by the upgraded Accompanist library. Using the BOM simplifies this alignment:implementation platform("androidx.compose:compose-bom:2022.03.00") // Add core Compose dependencies (like material, ui, etc.) below without version numbersClean and rebuild your project
Cached old dependencies can linger and cause issues even after updates. Run this command in your terminal:./gradlew cleanOr use Android Studio’s
Build → Clean Projectoption, followed byBuild → Rebuild Project.
Verification
After making these changes, re-launch your app. The ClassCastException should vanish, and your AnimatedNavHost will work smoothly with your single-Activity Compose setup.
内容的提问来源于stack exchange,提问作者Sparsh Dutta

