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

迁移至AndroidX后出现AppBarLayout加载运行时错误,求解决方案

问题原因与解决方法

Hey there! I totally get how frustrating it is to hit runtime roadblocks right after migrating to AndroidX—let’s break this down and fix it together.

🔍 问题根源

After switching to AndroidX, your XML layout is still using the old Support Library version of AppBarLayout (android.support.design.widget.AppBarLayout), but AndroidX relies on its own Material Components classes. Adding the old com.android.support:design:28.0.0 dependency doesn’t help because AndroidX and the legacy Support Library can’t coexist—they’re incompatible, which triggers the inflation crash when your app tries to load the layout.

🛠️ 分步解决方法

  1. Update the XML layout to use AndroidX's AppBarLayout
    Replace line 18 in your cups.xml file from:

    <android.support.design.widget.AppBarLayout android:id="@+id/cupsAppBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay">
    

    To the AndroidX equivalent:

    <com.google.android.material.appbar.AppBarLayout android:id="@+id/cupsAppBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay">
    
  2. Swap the old Support dependency for AndroidX Material
    Remove this line from your module-level build.gradle file:

    implementation 'com.android.support:design:28.0.0'
    

    Add the AndroidX Material Components dependency (use the latest stable version, e.g., 1.9.0):

    implementation 'com.google.android.material:material:1.9.0'
    
  3. Ensure your theme is Material-compatible
    Double-check that your app’s theme (including AppTheme.AppBarOverlay) is based on an AndroidX Material theme. For example, in styles.xml:

    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Your custom theme attributes here -->
    </style>
    

    Using a non-Material theme can cause inflation issues with Material Components like AppBarLayout.

  4. Clean and rebuild your project
    Head to Build > Clean Project in Android Studio, followed by Build > Rebuild Project. This clears cached old code and dependencies, making sure your new changes take full effect.

Once you’ve completed these steps, your app should inflate the AppBarLayout correctly without runtime crashes.

内容的提问来源于stack exchange,提问作者Hamed Salimian

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 16:52:38