AndroidX編譯 ConstraintLayout 報找不到類錯誤

今天Android studio 使用Androidx包來編譯的時候
報了一個這個錯誤java

Error inflating class android.support.constraint.ConstraintLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2632)
        at android.app.ActivityThread.access$700(ActivityThread.java:183)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1485)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5674)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.yisingle.print.label.base.BaseActivity.onCreate(BaseActivity.java:45)
        at android.app.Activity.performCreate(Activity.java:6155)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2499)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2632) 
        at android.app.ActivityThread.access$700(ActivityThread.java:183) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1485) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5674) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758) 

開始我覺得你沒引入包
後來發現gradle是引入了的包的,不是這個緣由 以下:android

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

而後我進入佈局文件發現app

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
</android.support.constraint.ConstraintLayout>

android.support.constraint.ConstraintLayoutoop

這個類點不進去,後面我才發現 ConstraintLayout包名改了。。 改爲 androidx.coordinatorlayout.widget.CoordinatorLayout了 而後修改一下佈局

<?xml version="1.0" encoding="utf-8"?>
android.support.constraint.ConstraintLayout 改爲  androidx.coordinatorlayout.widget.CoordinatorLayout
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
</androidx.coordinatorlayout.widget.CoordinatorLayout>

最後總結下 這個是AndroidStudio自帶的生成代碼的bug 若是使用AndroidX來編譯的 ConstraintLayout 在佈局文件中全名是androidx.coordinatorlayout.widget.CoordinatorLayout 再也不是android.support.constraint.ConstraintLayout 請注意gradle

相關文章
相關標籤/搜索