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

时间:2018-10-18 21:07:42

标签: android androidx

我刚刚通过Android Studio菜单选项重构-> 重构为AndroidX

迁移到androidx

我遇到以下错误:

  

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

有什么主意吗?

Stacktrace:

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)

21 个答案:

答案 0 :(得分:52)

我通过更改所有出现的

解决了我的问题
  

androidx.constraintlayout.ConstraintLayout

  

androidx.constraintlayout.widget.ConstraintLayout

答案 1 :(得分:30)

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

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

存储库为mavenCentral()

答案 2 :(得分:8)

我遇到了同样的问题,我通过以下方式解决了该问题:

在您的家属中是否已添加

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

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

androidx.constraintlayout.widget.ConstraintLayout

答案 3 :(得分:4)

如果在您的Gradle文件中添加 mavenCentral()作为存储库并将needs更改为androidx.constraintlayout.ConstraintLayout后仍无法解决问题,请尝试执行使缓存无效然后从文件菜单重新启动。它对我有用。

答案 4 :(得分:2)

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

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

并且已对

进行了更改
<androidx.constraintlayout.widget.ConstraintLayout instead of

<android.support.constraint.ConstraintLayout

答案 5 :(得分:1)

由于我的实现已经是 androidx.constraintlayout.ConstraintLayout,所以使用 Clean Project 就解决了我的问题。

答案 6 :(得分:1)

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

答案 7 :(得分:1)

请确保更改为

<androidx.constraintlayout.widget.ConstraintLayout

代替

<android.support.constraint.ConstraintLayout

<androidx.constraintlayout.widget.Barrier

代替

<android.support.constraint.Barrier

答案 8 :(得分:1)

确保您的项目完全迁移到androidx,以我为例:

<android.support.constraint.ConstraintLayout

代替

  <androidx.constraintlayout.widget.ConstraintLayout

所以改变它,我的问题就解决了!

答案 9 :(得分:1)

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

答案 10 :(得分:0)

之后我有同样的问题

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

我还使用:

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

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

我通过在书法初始化中添加一个字符串.disableCustomViewInflation()来解决此问题:

@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)感谢您的出色工作)也可以将其修复。

答案 11 :(得分:0)

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

如果没有,依赖项将以黄色突出显示,您可以通过将鼠标悬停在其上来进行更改。

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

  

androidx.constraintlayout.widget.ConstraintLayout

为获得更安全的选择,请清理您的项目并在完成上述步骤后再次同步。

答案 12 :(得分:0)

我更新了依赖关系,但无法解决错误。 然后我做了一个干净的项目并构建,并且现在可以正常工作。

答案 13 :(得分:0)

替换来自{strong> AndroidX库

ConstraintLayout
androidx.constraintlayout.ConstraintLayout

Android支持库

中的一个
android.support.constraint.ConstraintLayout

答案 14 :(得分:0)

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

enter image description here

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

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

答案 15 :(得分:0)

对我来说,更换gradle时此问题已解决

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

implementation "androidx.constraintlayout:constraintlayout:1.1.3"

同步然后清理项目

那太蠢了

答案 16 :(得分: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。

答案 17 :(得分:0)

build.gradle =>检查依赖项版本

implementation 'androidx.constraintlayout:constraintlayout:1.1.1'


进行更改

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

解决了问题

答案 18 :(得分:0)

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

答案 19 :(得分:0)

将约束布局的依赖关系更改为实现'androidx.constraintlayout:constraintlayout:2.0.2'

答案 20 :(得分:-8)

布局xml文件中的问题

更改此androidx.constraintlayout.ConstraintLayout  线性布局 检查您的日志