You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose中AnimatedNavHost抛出java.lang.ClassCastException异常求助

Fixing ClassCastException: ArrayList cannot be cast to Set with Accompanist AnimatedNavHost

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

  1. 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-rc is a reliable pick:

    implementation "com.google.accompanist:accompanist-navigation-animation:0.24.10-rc"
    
  2. 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 numbers
    
  3. Clean and rebuild your project
    Cached old dependencies can linger and cause issues even after updates. Run this command in your terminal:

    ./gradlew clean
    

    Or use Android Studio’s Build → Clean Project option, followed by Build → 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 11:37:38