AndroidStudio3.5.2,因爲將工程轉爲Androidx,所以出現了很多的錯誤,其中有一條便是界面在design設計器裏無法顯示正常,灰色了,完整錯誤如下:android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.constraintlayout.ConstraintLayout
很明顯,錯誤在於佈局文件,但是錯在哪?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.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=".ResultActivity">
</androidx.constraintlayout.ConstraintLayout>
後面發現是因爲這個ConstraintLayout應該是widget裏面的,這裏因爲自動轉化,搞錯了包含文件的層次,正確的做法,如下:
添加widget包,放在Constraint前面,大家可以逐步去搜一下對應的佈局頭文件的API層次結構。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ResultActivity">
</androidx.constraintlayout.widget.ConstraintLayout>