【问题标题】:error inflating ConstraintLayout in android studio在android studio中膨胀ConstraintLayout时出错
【发布时间】:2017-07-19 00:25:35
【问题描述】:

我在使用 Android Studio 时遇到问题。我最近升级了我的 SDK 管理器以包含;

compile 'com.android.support.constraint:constraint-layout:1.0.0-beta5'

类路径是

 classpath 'com.android.tools.build:gradle:2.2.3'

项目构建良好。 Intellisense 和文档工作正常。但是当我尝试在我的手机上运行(调试)该应用程序时,我收到了这个错误;

 Caused by: android.view.InflateException: Binary XML file line #2:
 Binary XML file line #2: Error inflating class ConstraintLayout
                   at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                   at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:443)
                   at android.app.Activity.setContentView(Activity.java:2172)

我已经同步了 gradle。我已经重新启动了 Android Studio。我已经清理并重建了项目。

关于如何解决这个问题的任何想法? 谢谢!

-编辑- XML 看起来像这样;

<ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/PLAY_PARENT"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/greenfelt2"
android:padding="0dp">

【问题讨论】:

  • 添加您的 xml 代码。

标签: android android-studio android-constraintlayout


【解决方案1】:

你应该添加如下

<android.support.constraint.ConstraintLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/PLAY_PARENT"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/greenfelt2"
 android:padding="0dp">

compile 'com.android.support.constraint:constraint-layout:1.0.2'

查看最新版本here

查看这个项目以了解更多关于ConstraintLayout Usage

更新

对于那些从android支持切换到androidx的人,记得将android.support.constraint.ConstraintLayout更改为androidx.constraintlayout.widget.ConstraintLayout

【讨论】:

  • 这样做会导致这个错误; “使用版本 1.0.0-beta5,这个版本的约束库已经过时了。这只是一个该死的智能感知错误。它仍在构建和运行......(我什至没有尝试运行,一旦我看到那个错误......) . 谢谢。
  • 查看此项目以了解有关 ConstraintLayout 的更多信息github.com/Arjun-sna/android-constraintlayout-demo
  • 如果有警告,请使用当前版本:'com.android.support.constraint:constraint-layout:1.0.2'
  • 只是一个更新。新版本的约束布局已经发布。 1.1.0
  • 从android支持切换到androidx的朋友记得把android.support.constraint.ConstraintLayout改成androidx.constraintlayout.widget.ConstraintLayout
【解决方案2】:

我不知道这是否是两年半前的解决方案,但今天我遇到了类似的错误。在这里找到答案:Error inflating class androidx.constraintlayout.ConstraintLayout after migration to androidx

基本上只有约束布局的xml标签应该重命名为这些->

androidx.constraintlayout.widget.ConstraintLayout

【讨论】:

    【解决方案3】:

    如果你使用androidx,你必须使用

    androidx.constraintlayout.widget.ConstraintLayout
    

    【讨论】:

      【解决方案4】:

      可能有很多原因

      1. 依赖版本问题(同上)

      2. 内存不足问题:在我的情况下,我为约束布局的背景添加了高分辨率图像,或者在同一页面(.xml 文件)中,我们将高分辨率图像用于任何 imageview 源.

      【讨论】:

        【解决方案5】:

        我也有同样的问题。对我来说,原因是我的项目“自动转换第三方库以使用 AndroidXhas”。该怎么办?只需以下两个步骤:

        第一步: 请检查您的 gradle.properties,如果您看到以下行,您可能遇到与我完全相同的问题。您可以先删除它们。

        android.useAndroidX=true
        android.enableJetifier=true
        

        第二步:在主要活动中,我改变了

        import androidx.core.app.ActivityCompat;
        import androidx.core.content.ContextCompat;
        

        进入

        import android.support.v4.app.ActivityCompat;
        import android.support.v4.content.ContextCompat;
        

        突然间一切正常!

        【讨论】:

        • 支持包已弃用,android 建议使用 androidx
        【解决方案6】:

        请使用 ConstraintLayout 依赖版本 '2.0.0-beta5',它已经解决了我的问题。

        【讨论】:

          【解决方案7】:

          我遇到了同样的问题,该应用程序在其他手机上运行正常,但在中兴通讯设备上出现此异常。我通过在 Android Studio 中禁用 Instant Run 来修复它。

          【讨论】:

            【解决方案8】:

            添加到你的 app/build.gradle

            dependencies {
                ...
                implementation 'com.android.support.constraint:constraint-layout:1.0.2'
                ...
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-09-14
              • 2020-05-10
              相关资源
              最近更新 更多