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

Android Studio启动Activity报错:ConstraintLayout加载失败(InflateException)

解决Android中ConstraintLayout Inflate错误但布局用RelativeLayout的问题

Hey there, let's tackle this inflate error you're facing. It's a common gotcha, but easy to fix once you spot the root cause!

First, let's break down the contradiction in your issue: the error says it can't inflate android.support.constraint.ConstraintLayout, but your layout's root is RelativeLayout. That means somewhere in your layout file (even as a child view), you're referencing ConstraintLayout, but your project doesn't have the necessary dependencies to support it—or there's a cache/naming issue at play.

Here are step-by-step fixes to resolve this:

  • Check your full layout code
    Double-check the entire XML file. Even if the root is RelativeLayout, you might have a child view using android.support.constraint.ConstraintLayout. If you don't actually need ConstraintLayout, replace that tag with a layout you already support (like RelativeLayout or LinearLayout).

  • Verify your dependency setup
    If you do need ConstraintLayout in your layout, make sure you've added the correct dependency to your app-level build.gradle file:

    • For the old Support Library (not recommended anymore):
      implementation 'com.android.support.constraint:constraint-layout:1.1.3'
      
    • For AndroidX (the modern, recommended approach):
      implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
      
      Also, if you're using AndroidX, make sure the ConstraintLayout tag in your XML uses the correct package path: androidx.constraintlayout.widget.ConstraintLayout instead of the old android.support.constraint.ConstraintLayout.
  • Clear Android Studio cache and rebuild
    Sometimes cached files cause weird inflate errors. Go to File > Invalidate Caches... and select Invalidate and Restart. This will clear stale cache files and force a full rebuild of your project.

  • Check for typos in layout tags
    It's easy to mistype a layout name. Double-check that you didn't accidentally write ConstraintLayout somewhere when you meant RelativeLayout, or misspell the package path for any layout tags.

For example, if your layout has a section like this:

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

</android.support.constraint.ConstraintLayout>
You can either replace that with a RelativeLayout, or add the corresponding ConstraintLayout dependency to your build.gradle.

Following these steps should resolve the inflate error you're seeing.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:03:46