【问题标题】:Can't inflate ConstraintLayout无法膨胀 ConstraintLayout
【发布时间】:2019-06-10 06:20:58
【问题描述】:

每次我的应用程序崩溃,因为它在类路径中找不到 Landroidx/constraintlayout/widget/R$styleable。我试图重建,使缓存无效,但它总是在运行时给我同样的错误。
我尝试了 1.1.2 和 1.1.3 两个版本。有趣的是:当使用 1.1.3 androidX Jetifier 时,只强制使用 1.1.2,正如您从 gradle 报告中看到的那样

Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
    at android.view.LayoutInflater.createView(LayoutInflater.java:651)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:496)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:141)
    at com.github.sharetaxi.LoginActivity.onCreate(LoginActivity.kt:24)
    at android.app.Activity.performCreate(Activity.java:7050)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1620)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6701)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:246)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
 Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/constraintlayout/widget/R$styleable;
    at androidx.constraintlayout.widget.ConstraintLayout.init(ConstraintLayout.java:590)
    at androidx.constraintlayout.widget.ConstraintLayout.<init>(ConstraintLayout.java:567)
    at java.lang.reflect.Constructor.newInstance0(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:651) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:496) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:141) 
    at com.github.sharetaxi.LoginActivity.onCreate(LoginActivity.kt:24) 
    at android.app.Activity.performCreate(Activity.java:7050) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2931) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1620) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:176) 
    at android.app.ActivityThread.main(ActivityThread.java:6701) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:246) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) 
 Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.constraintlayout.widget.R$styleable" on path: DexPathList

我正在使用带有全新 android gradle 插件的 Android Studio 3.3。在我的 gradle.properties 中,我启用了 jetifier:

android.useAndroidX=true
android.enableJetifier=true

依赖解决如下:

> Task :app:dependencyInsight
androidx.constraintlayout:constraintlayout:1.1.2
   variant "default" [
      org.gradle.status                                            = release (not requested)
      Requested attributes not found in the selected variant:
         com.android.build.api.attributes.BuildTypeAttr               = debug
         org.gradle.usage                                             = java-api
         com.android.build.gradle.internal.dependency.AndroidTypeAttr = Aar
         org.jetbrains.kotlin.platform.type                           = androidJvm
   ]
   Selection reasons:
      - Was requested
      - Selected by rule : ENABLE_JETIFIER is enabled
      - By constraint : debugRuntimeClasspath uses version 1.1.2

com.android.support.constraint:constraint-layout:1.1.3 -> androidx.constraintlayout:constraintlayout:1.1.2
+--- debugCompileClasspath
\--- project :general
     \--- debugCompileClasspath

(*) - dependencies omitted (listed previously)

A web-based, searchable dependency report is available by adding the --scan option.

在 xml 中如下所示:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_bg"
    android:orientation="vertical">
...
</androidx.constraintlayout.widget.ConstraintLayout>

更新:

最后我不得不使用 ConstraintLayout 2.0 alpha 版本,因为没有找到其他解决方案

【问题讨论】:

  • 我遇到了同样的问题。我只是将ConstraintLayout 更新为 1.1.3,错误就消失了。无需使用 alpha 版本

标签: android android-constraintlayout androidx


【解决方案1】:

确保将实现 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3' 放入 build.gradle (app) 的依赖项中:

dependencies {
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
}

在 gradle.properties 里面添加:

android.enableJetifier=true
android.useAndroidX=true

它适用于 Android Studio 3.3。

编辑:我刚刚注意到您不想使用 alpha 版本。无论如何,我希望我的答案的其他部分能有所帮助。

【讨论】:

  • 我不得不从约束布局 1.x 迁移到 2.0.0(根据这个答案)来解决这个问题。仅供参考,最新的 alpha 版本是 alpha4
【解决方案2】:

您的依赖项似乎是 com.android.support.constraint 而不是 androidx.constraintlayout

为我改变

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

修复了问题。

【讨论】:

    【解决方案3】:

    在我的情况下,帮助只是遵循 IDE 的建议 - 这些不仅是更新 constraint 依赖项,还包括所有其他 androidx 依赖项。

    附带说明 - 我在 Android Studio 3.3 上从 Android Studio 生成器启动示例应用时偶然发现了这一点。

    【讨论】:

      【解决方案4】:

      除了使用

      implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
      

      我还必须在 Andriod Studio 中重建项目以使其正常工作

      【讨论】:

        【解决方案5】:

        TL;DR

        The issue can be totally different from what the error message shows


        虽然我的案例可能特别独特,但我会分享它以供参考。

        我通过Titanium Hyperlooplink 创建了一个要在另一个项目中使用的库,您必须在其中列出所有使用的库(aar 或 jar)。

        该问题与显示的错误消息完全无关。我花了很长时间才发现图书馆 (gson-2.8.6jar) 不见了。

        【讨论】:

          猜你喜欢
          • 2021-09-03
          • 1970-01-01
          • 1970-01-01
          • 2017-07-19
          • 1970-01-01
          • 2015-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多