通过使用Android Studio菜单选项“重构 -> 重构为AndroidX”迁移到Androidx软件包后,我遇到了以下错误:
Error inflating class androidx.constraintlayout.widget.ConstraintLayout
请确保将约束布局更新至最新版本:
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
替换您的XML标签名称
<androidx.constraintlayout.ConstraintLayout>
带有
<androidx.constraintlayout.widget.ConstraintLayout>
在每个出现此快捷方式的地方:
Windows: CTRL + SHIFT + R
Mac: COMMAND + SHIFT + R
在AndroidStudio 3.3 RC2中,我通过升级解决了它 -
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
在build.gradle(模块:app)中只需替换
<androidx.constraintlayout.ConstraintLayout>
使用
<androidx.constraintlayout.widget.ConstraintLayout>
在您的项目xml文件中,您可以使用Mac上的⇧⌘F或⇧⌘R快捷键,在Windows上使用Ctrl+Shift+R进行路径替换。
嗯,在我的情况下,我尝试了解决方案1,但仍然没有起作用。 然后我使用了解决方案2以及解决方案1。
在应用程序级别的build.gradle
文件中更新当前版本的ConstraintLayout
。
从
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
进入
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
在应用解决方案 1后仍未生效,可以尝试清除缓存并重启 Android Studio。
文件(File) → 无效缓存/重启(Invalidate Caches / Restart) → 无效并重启(Invalidate and Restart)
我尝试了这两种方法,对我来说都有效。
注意: 解决方案 1 是必要的。
1.1.3
版本,希望您的问题能够得到解决。implementation 'androidx.constraintlayout:constraintlayout:1.1.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'
<androidx.constraintlayout.widget.ConstraintLayout
...
>
</androidx.constraintlayout.widget.ConstraintLayout>
不要混淆这两个。
build.gradle
中约束布局的版本来解决它:
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
它于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视频获得。implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
ConstraintLayout
的android:background="@drawable/bg_image"
更改为SVG图像,结果出现了这个异常。 - CoolMind