在解析类androidx.constraintlayout.widget.ConstraintLayout时出错。

53

通过使用Android Studio菜单选项“重构 -> 重构为AndroidX”迁移到Androidx软件包后,我遇到了以下错误:

Error inflating class androidx.constraintlayout.widget.ConstraintLayout
15个回答

113

请确保将约束布局更新至最新版本:

implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

替换您的XML标签名称

<androidx.constraintlayout.ConstraintLayout>

带有

<androidx.constraintlayout.widget.ConstraintLayout>

在每个出现此快捷方式的地方:

Windows: CTRL + SHIFT + R

Mac: COMMAND + SHIFT + R


2
同样的错误也可能在android.constraintlayout.Guideline中发生,所以也要注意。 - lbenedetto
需要使用androidx.coordinatorlayout.widget.CoordinatorLayout来实现BottomAppBar。有任何解决方案吗? - Akshay
我在使用 androidx.constraintlayout.motion.MotionLayout 时遇到了同样的问题。将其重命名为 androidx.constraintlayout.motion.widget.MotionLayout 后,我的代码得以编译并成功运行。 - user8425970
在我的情况下:constraintlayout:1.1.2 → constraintlayout:1.1.3。 - Naveen Kumawat

35

在AndroidStudio 3.3 RC2中,我通过升级解决了它 -

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
在build.gradle(模块:app)中
注意:此解决方案也适用于版本3.3(根据FireZenk的以下评论),3.5.2(根据user2350644的以下评论)。

3
同样适用于3.3版本发布版本。 - FireZenk
1
我在升级到最新的Android Studio版本3.5.2和gradlePluginVersion 3.5.2后遇到了这个异常。 你的解决方案完美地解决了我的问题,谢谢。 - ice_chrysler

25

只需替换

<androidx.constraintlayout.ConstraintLayout>

使用

<androidx.constraintlayout.widget.ConstraintLayout>

在您的项目xml文件中,您可以使用Mac上的⇧⌘F或⇧⌘R快捷键,在Windows上使用Ctrl+Shift+R进行路径替换

enter image description here


10

嗯,在我的情况下,我尝试了解决方案1,但仍然没有起作用。 然后我使用了解决方案2以及解决方案1


解决方案1

在应用程序级别的build.gradle文件中更新当前版本的ConstraintLayout

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

进入

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

解决方案 2

在应用解决方案 1后仍未生效,可以尝试清除缓存并重启 Android Studio。

文件(File)无效缓存/重启(Invalidate Caches / Restart)无效并重启(Invalidate and Restart)

我尝试了这两种方法,对我来说都有效。

注意: 解决方案 1 是必要的。


4
我也遇到了这个问题。此问题仅出现在 constraintlayout 依赖版本 中。请使用 1.1.3 版本,希望您的问题能够得到解决。
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

3

添加ConstraintLayout的方法有两种:

implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha3'

那么你需要像这样使用它:

<android.support.constraint.ConstraintLayout
        ...>

</android.support.constraint.ConstraintLayout>

或者使用androidx:

implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'

那么你需要使用 <\p>。
<androidx.constraintlayout.widget.ConstraintLayout
        ...
        >

</androidx.constraintlayout.widget.ConstraintLayout>

不要混淆这两个。


将constrainlayout库从1.1.3更新到2.0.0-beta2解决了我的问题。 - fklappan
太棒了!这是完美的答案,解决了我的问题!!!我按照你说的使用了正确的语法!!! - Thanasis

2
自从Android Studio更新到3.3版本之后,会出现这个错误。我通过升级应用级别的build.gradle中约束布局的版本来解决它: implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

1
我使用的是Android Studio 4.1和这个约束布局库:

implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

它于09-Nov-20更新,但遇到了与充气约束布局相同的错误。在我的Samsung a40上,应用程序可以正常工作,但在我的ZTE nubia n1上,应用程序会立即崩溃。我发现问题出在我的布局背景图像上,因为我的png图像对于一个具有不同屏幕分辨率的旧设备(而非Samsung a40)来说太大了。我通过文件浏览器(drawable - 右键单击 - 在资源管理器中显示)导入了一个背景png图像,事实上确实是出了问题。我尝试使用Android Drawable Importer导入图像,但我注意到这个插件在Android Studio上不再起作用。与Android Drawable Importer相关的解决方案:如果您已经在Android Studio上安装了Android Drawable Importer,请在开始修复过程之前卸载它。要修复Android Drawable Importer,您需要AD-hack-plugin,您可以通过关注Thomas Samoul的YouTube视频获得。
在修复Android Drawable Importer之后,只需使用它导入您的图像,一切都会正常工作。

同样适用于SVG图像。我将ConstraintLayoutandroid:background="@drawable/bg_image"更改为SVG图像,结果出现了这个异常。 - CoolMind
是的,最大的问题是在某些设备上一切都正常工作,而在另一些设备上则会崩溃。 我没有在模拟器上尝试过,但我想应该不会有任何崩溃。 - Dezo
1
同意你的观点。但是我在模拟器上使用SVG时遇到了这个异常。 - CoolMind

1

0
在我的情况下,我只需要将 androidx 依赖更新到最新版本。 编辑 在 build.gradle (app) 中,你需要查找包含 androidx 的依赖项。Android Studio 将显示有关这些依赖项的最新版本信息。使用这些信息,只需更改依赖项的版本即可。

请解释如何做。 - GabrieleMartini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接