【问题标题】:Error inflating class androidx.constraintlayout.widget.ConstraintLayout膨胀类 androidx.constraintlayout.widget.ConstraintLayout 时出错
【发布时间】:2018-11-19 21:10:39
【问题描述】:

使用 Android Studio 菜单选项 Refactor -> Refactor to Androidx...迁移到 Androidx 包后...

我收到以下错误:

Error inflating class androidx.constraintlayout.widget.ConstraintLayout

【问题讨论】:

    标签: android androidx


    【解决方案1】:

    确保约束布局更新到最新版本:

    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>

    【讨论】:

    • android.constraintlayout.Guideline 也可能出现同样的错误,所以也要留意。
    • BottomAppBar需要使用androidx.coordinatorlayout.widget.CoordinatorLayout。任何解决方案。
    • 我在使用 androidx.constraintlayout.motion.MotionLayout 时遇到了同样的问题。将其重命名为 androidx.constraintlayout.motion.widget.MotionLayout 编译并为我工作。
    • 在我的例子中:constraintlayout:1.1.2 -> constraintlayout:1.1.3.
    【解决方案2】:

    在 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 发行版也有效
    • 我在升级到最新的 Android Studio 版本 3.5.2 和 gradlePluginVersion 3.5.2 后遇到了这个异常。您的解决方案完美运行,谢谢。
    【解决方案3】:

    只需替换

    <androidx.constraintlayout.ConstraintLayout>
    

    <androidx.constraintlayout.widget.ConstraintLayout>
    

    在您项目的 xml 文件中。

    你可以在mac (⇧⌘F or ⇧⌘R)和windows中使用Replace in Path快捷键(Ctrl+Shift+R)

    【讨论】:

      【解决方案4】:

      在我的情况下,我尝试了 解决方案 1,但仍然无法正常工作。然后我使用 Solution 2Solution 1


      解决方案 1

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

      来自

      implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
      

      进入

      implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
      

      解决方案 2

      使缓存无效并重新启动 Android Studio(如果在应用 解决方案 1 后仍然无法正常工作)。

      文件使缓存无效/重新启动无效并重新启动

      这两个我都用过,对我来说效果很好。

      注意: 解决方案 1 很重要。

      【讨论】:

        【解决方案5】:

        我也遇到过这个问题。此问题仅发生在 constraintlayout 依赖版本。 使用1.1.3 版本并希望您的问题能够得到解决:

        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        

        【讨论】:

          【解决方案6】:

          添加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>
          

          不要将这两者混为一谈。

          【讨论】:

          • 将约束布局库从 1.1.3 更新到 2.0.0-beta2 解决了我的问题。
          • 太棒了!这是完美的答案并将我整理出来!我像你说的那样使用了正确的语法!!!
          【解决方案7】:

          由于 Android Studio 更新到 3.3 会弹出这个错误。我通过升级应用级别的build.gradle中的约束布局版本解决了这个问题:

          implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

          implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

          【讨论】:

            【解决方案8】:

            我使用 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 后,只需使用它导入您的图像,一切都会正常工作。

            【讨论】:

            • SVG 图像也是如此。我将 ConstraintLayoutandroid:background="@drawable/bg_image" 更改为 SVG 图像并得到了这个异常。
            • 是的,最大的问题是在某些设备上一切正常,而在某些应用程序上崩溃。我没有尝试模拟器,但我想不会有任何崩溃。
            • 同意你的看法。但是我在带有 SVG 的模拟器上遇到了这个异常。
            【解决方案9】:

            我遇到了同样的问题,上面的解决方案都没有奏效。这个问题其实是由于我切换到 NoActionBar 主题,而应用程序使用 ActionBar 来 setTitle() 时抛出的空指针异常引起的。 The linked stack overflow answer helped me figure it out

            【讨论】:

              【解决方案10】:

              Android Studio 更新到 3.3 版。请移除之前版本的约束布局

              我的案例使用了这个

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

              谢谢

              【讨论】:

                【解决方案11】:

                就我而言,我只需要将 androidx 依赖项更新到最新版本。

                EDIT 在 build.gradle (app) 中,您必须查找包含 androidx 的依赖项。 Android Studio 将向您展示有关这些依赖项的最新版本的信息。使用该信息只需更改依赖版本。

                【讨论】:

                • 请解释一下如何做
                【解决方案12】:

                在 AndroidStudio 3.4 中我不得不降级到

                implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
                

                让它再次工作。

                【讨论】:

                  【解决方案13】:

                  奇怪的是,当我将androidx.appcompat:...' 库升级到

                  'androidx.appcompat:appcompat:1.1.0-alpha05' 带约束布局版本 'androidx.constraintlayout:constraintlayout:1.1.3' 在 Android Studio 3.3 上

                  【讨论】:

                    【解决方案14】:

                    对于那些尝试了以上所有方法都没有白费的人,降级 constraintLayout 对我有用(在我绞尽脑汁 3 天尝试在阳光下做所有事情之后)。

                    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
                    

                    implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
                    

                    【讨论】:

                      猜你喜欢
                      • 2020-07-28
                      • 1970-01-01
                      • 1970-01-01
                      • 2019-11-29
                      • 2017-02-28
                      • 2021-07-10
                      • 2020-08-01
                      • 2016-02-28
                      • 2013-05-17
                      相关资源
                      最近更新 更多