【发布时间】:2018-11-19 21:10:39
【问题描述】:
使用 Android Studio 菜单选项 Refactor -> Refactor to Androidx...迁移到 Androidx 包后...
我收到以下错误:
Error inflating class androidx.constraintlayout.widget.ConstraintLayout
【问题讨论】:
使用 Android Studio 菜单选项 Refactor -> Refactor to 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 kbd>
Mac:COMMAND + SHIFT + R kbd>
【讨论】:
在 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 的以下评论)
【讨论】:
只需替换
<androidx.constraintlayout.ConstraintLayout>
与
<androidx.constraintlayout.widget.ConstraintLayout>
在您项目的 xml 文件中。
你可以在mac (⇧⌘F or ⇧⌘R)和windows中使用Replace in Path快捷键(Ctrl+Shift+R)
【讨论】:
在我的情况下,我尝试了 解决方案 1,但仍然无法正常工作。然后我使用 Solution 2 和 Solution 1。
在应用级build.gradle 文件中更新当前版本ConstraintLayout。
来自
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
进入
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
使缓存无效并重新启动 Android Studio(如果在应用 解决方案 1 后仍然无法正常工作)。
文件 → 使缓存无效/重新启动 → 无效并重新启动
这两个我都用过,对我来说效果很好。
注意: 解决方案 1 很重要。
【讨论】:
我也遇到过这个问题。此问题仅发生在 constraintlayout 依赖版本。
使用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>
不要将这两者混为一谈。
【讨论】:
由于 Android Studio 更新到 3.3 会弹出这个错误。我通过升级应用级别的build.gradle中的约束布局版本解决了这个问题:
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
到
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
【讨论】:
我使用 Android Studio 4.1 和这个库进行约束布局:
实现'androidx.constraintlayout:constraintlayout:2.0.4'
于 20 年 11 月 9 日更新,在膨胀约束布局时出现同样的错误。
在我的情况下,我的三星 a40 应用程序可以运行,但我的中兴 nubia n1 应用程序立即崩溃。 我认为我的布局背景图像存在问题,因为我的 png 图像对于屏幕分辨率与三星 a40 不同的旧设备来说太大了。 我通过文件资源管理器(可绘制-右键单击-在资源管理器中显示)导入了背景png图像,实际上存在问题。
我尝试使用 Android Drawable Importer 导入图像,但我注意到这个插件在 Android Studio 上不再工作。
与安卓绘图导入器相关的解决方案:
如果您已经在 Android Studio 上安装了 Android Drawable Importer,请在开始修复过程之前将其卸载。 要修复 Android Drawable Importer,您需要 ADI-hack-plugin,您可以通过关注Thomas Samoul 的这个 youtube 视频获得它。
修复 Android Drawable Importer 后,只需使用它导入您的图像,一切都会正常工作。
【讨论】:
ConstraintLayout 的 android:background="@drawable/bg_image" 更改为 SVG 图像并得到了这个异常。
我遇到了同样的问题,上面的解决方案都没有奏效。这个问题其实是由于我切换到 NoActionBar 主题,而应用程序使用 ActionBar 来 setTitle() 时抛出的空指针异常引起的。 The linked stack overflow answer helped me figure it out
【讨论】:
Android Studio 更新到 3.3 版。请移除之前版本的约束布局
我的案例使用了这个
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
谢谢
【讨论】:
就我而言,我只需要将 androidx 依赖项更新到最新版本。
EDIT 在 build.gradle (app) 中,您必须查找包含 androidx 的依赖项。 Android Studio 将向您展示有关这些依赖项的最新版本的信息。使用该信息只需更改依赖版本。
【讨论】:
在 AndroidStudio 3.4 中我不得不降级到
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
让它再次工作。
【讨论】:
奇怪的是,当我将androidx.appcompat:...' 库升级到
'androidx.appcompat:appcompat:1.1.0-alpha05' 带约束布局版本
'androidx.constraintlayout:constraintlayout:1.1.3' 在 Android Studio 3.3 上
【讨论】:
对于那些尝试了以上所有方法都没有白费的人,降级 constraintLayout 对我有用(在我绞尽脑汁 3 天尝试在阳光下做所有事情之后)。
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
到
implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
【讨论】: