迁移到androidx后,膨胀类androidx.constraintlayout.ConstraintLayout时出错


87

我刚刚通过Android Studio菜单选项Refactor- > Refactor to AndroidX迁移到androidx

我收到以下错误:

android.view.InflateException:二进制XML文件第2行:二进制XML文件第2行:错误膨胀了类androidx.constraintlayout.ConstraintLayout

任何的想法?

堆栈跟踪:

2018-10-19 00:25:58.128 28131-28131/com.midounoo.midounoo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.midounoo.midounoo, PID: 28131
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.midounoo.midounoo/com.midounoo.midounoo.Base.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.constraintlayout.ConstraintLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6255)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
     Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.constraintlayout.ConstraintLayout
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.constraintlayout.ConstraintLayout
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.constraintlayout.ConstraintLayout" on path: DexPathList[[zip file "/data/app/com.midounoo.midounoo-2/base.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.midounoo.midounoo-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.midounoo.midounoo-2/lib/arm64, /system/lib64, /vendor/lib64, /system/vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:609)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.midounoo.midounoo.Base.MainActivity.onCreate(MainActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:6666)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6255)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)

请显示整个堆栈跟踪。
TheWanderer '18 -10-18

请分享您的应用程序级别构建gradle,以便我们可以查看相关性。
Abhinav Saxena

Answers:


177

我通过更改所有出现的问题来解决我的问题

androidx.constraintlayout.ConstraintLayout

androidx.constraintlayout.widget.ConstraintLayout


12
是的,Android Studio转换中的错误,必须使用androidx.constraintlayout.widget.ConstraintLayout更改xml布局中的所有出现…谢谢
issamux

1
我已经使用androidx支持库创建了一个新项目,并且我的主要活动中已经有了androidx.constraintlayout.widget.ConstraintLayout,但是仍然遇到相同的错误。
Kanchan

2
@Kanchan如果您在多模块项目中工作,请检查是否需要更新任何内容中的ConstraintLayout路径。
Vasudev

50

添加androidx.constraintlayout.widget.ConstraintLayout到依赖项:

dependencies  {
    // https://mvnrepository.com/artifact/androidx.constraintlayout/constraintlayout
    implementation "androidx.constraintlayout:constraintlayout:1.1.3"
}

在上可用mavenCentral()


第二个constraintlayout不是以大写开头C。正确'androidx.constraintlayout:constraintlayout:1.1.3'
ʍѳђઽ9ท

您使我免于轻度头痛
Koen Van Looveren

1
为什么在Android Studio上会发生这种情况?我将其从1.1.2更改为1.1.3。谢谢。
BollMose19年

1
@BollMose-我不知道为什么,但是1.1.2只是cho死。可能是Google错过的好人所缺少的一些兼容性程序。他们倾向于这样做很多;欢迎使用Android编程!
SMBiggs

2
对我来说,这应该是唯一可接受的答案。有一个错误包含1.1.2,即1.1.3,可以解决此问题。
mochadwi

10

我遇到了同样的问题,我按以下步骤解决了它:

在您的依赖中,如果您添加了

implementation 'androidx.constraintlayout:constraintlayout:1.x.x' 那是正确的

但是在您的xml布局文件中,您必须将小部件用作

androidx.constraintlayout.widget.ConstraintLayout


5

如果在您的Gradle文件中将mavenCentral()作为存储库添加并更改androidx.constraintlayout.ConstraintLayoutandroidx.constraintlayout.widget.ConstraintLayout不能解决问题后,请尝试执行Invalidate cache并从文件菜单重新启动。它为我工作。


5

build.gradle =>检查依赖版本

implementation 'androidx.constraintlayout:constraintlayout:1.1.1'


改变

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

解决了问题


1
它的格式很好,这个答案对我有用。谢谢。
卢卡斯·宾巴

来自1.1.2。更新到1.1.3也为我修复了此问题。谢谢!
星期一

5

确保您的项目完全迁移到androidx,就我而言,我发现:

<android.support.constraint.ConstraintLayout

代替

  <androidx.constraintlayout.widget.ConstraintLayout

因此,更改它并解决我的问题!


4

我有以下同样的问题:

  • 迁移支持库到androidx;
  • 将targetSdkVesrsion增加到29;

我还使用:

implementation "uk.co.chrisjenx:calligraphy:2.3.0"

我尝试过此问题的所有帖子,但没有成功。

我通过.disableCustomViewInflation()在Calligraphy init中添加一个字符串来解决此问题:

@Override
public void onCreate() {

    super.onCreate();
    // ...
    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
            .setDefaultFontPath("fonts/Sans-Regular.ttf")
            .setFontAttrId(R.attr.fontPath)
            .disableCustomViewInflation() // <----- this fix
            .build());
    // ...
}

我希望书法的下一个发行版(克里斯托弗·詹金斯(Christopher Jenkins)感谢您的出色工作)也可以将其修复。


我什至没有在我的项目中使用它,但是该库位于gradle中。删除后,应用程序开始按预期运行。谢谢
Pratik Mhatre

4

单击之后Refactor -> Migrate to AndroidX,确保将build.gradle(Module:app)中的所有依赖项都标记为最新版本。

如果不是,则相关性将以黄色突出显示的颜色显示,您可以通过将鼠标悬停在其上来进行更改。

其次,将所有XML布局文件中的ConstraintLayout标记更改为

androidx.constraintlayout.widget.ConstraintLayout

为了获得更安全的选择,请清理您的项目,然后在上述步骤之后再次进行同步。


2

确保您已添加约束布局的依赖项

 dependencies  {
    implementation "androidx.constraintlayout:constraintlayout:1.1.3"
}

并已对

<androidx.constraintlayout.widget.ConstraintLayout instead of

<android.support.constraint.ConstraintLayout

1

我从xml复制并粘贴了现有的ConstraintLayout标记,这对我造成了问题。只需从头开始删除并重新添加AndroidStudio中的ConstraintLayout标签,就可以解决此问题。


1

确保更改为

<androidx.constraintlayout.widget.ConstraintLayout

代替

<android.support.constraint.ConstraintLayout

<androidx.constraintlayout.widget.Barrier

代替

<android.support.constraint.Barrier

1

我在Android Studio 4.0中遇到此问题。我使用android studio 4.0几天没有问题。设计师的观点似乎无处可去。我只能通过卸载android studio并安装最新的android studio版本来解决此问题。


0

是的 mavenCentral()和正确的依赖关系解决了我同样的问题


0

我有一个类似的错误。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2757)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2818)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:163)
        at android.app.ActivityThread.main(ActivityThread.java:6393)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
    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:430)
        at android.view.LayoutInflater.createView(LayoutInflater.java:652)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.myapp.MainActivity.onCreate(MainActivity.java:23)
        at android.app.Activity.performCreate(Activity.java:6858)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2710)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2818)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:163)
        at android.app.ActivityThread.main(ActivityThread.java:6393)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
    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)
        ... 23 more
    Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.constraintlayout.widget.R$styleable" on path: DexPathList[[zip file "/data/app/com.myapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp-1/lib/arm64, /system/lib64, /vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        ... 25

遵循以下建议:

add androidx.constraintlayout.ConstraintLayout to the dependencies:

dependencies  {
    implementation "androidx.constraintlayout:constraintlayout:1.1.3"
}

还有存储库mavenCentral()。

为我工作。我可以编译APK。


0

我更新了依赖项,但无法解决该错误。然后,我做了一个干净的项目并构建,并且它现在正在运行。


0
  1. 查找所有默认值,然后单击“重构”->“重构为AndroidX”
  2. 打开此https://developer.android.com/jetpack/androidx/migrate/class-mappings并找到“ android.support.v4.widget.DrawerLayout”。在同一行中,AndroidX类名是“ androidx.drawerlayout.widget.DrawerLayout”,将其复制。

在此处输入图片说明

  1. 将项目代码,布局和菜单中的所有“ android.support.v4.widget.DrawerLayout”替换为“ androidx.drawerlayout.widget.DrawerLayout”。所有替换都将起作用。

我的意思是找到映射并在整个项目中使用它。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.