ConstraintLayout 不能与 Android XML Preview 中的其他类一起实例化

Posted

技术标签:

【中文标题】ConstraintLayout 不能与 Android XML Preview 中的其他类一起实例化【英文标题】:ConstraintLayout cannot be instantiated along with other classes in Android XML Preview 【发布时间】:2020-11-09 19:43:05 【问题描述】:

有些类没有在我的 android Studio XML Preview 中呈现(它们只是显示一个带有类名的灰色矩形)。我认为我使用的库版本之间存在一些不兼容。这是我的 XML 布局。

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
        <variable
            name="user"
            type="com.mobile.githubuser.model.GithubUserProfileSummary" />
    </data>

    <androidx.core.widget.NestedScrollView
        android:layout_
        android:layout_
        android:fillViewport="true">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_
            android:layout_
            android:orientation="vertical">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/act_details_toolbar"
                android:layout_
                android:layout_
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:title="@string/act_user_details_toolbar_title" />

            <com.google.android.material.imageview.ShapeableImageView
                style="@style/Widget.GithubUser.ShapeableImageView.Circular"
                android:id="@+id/shapeableImageView"
                android:layout_
                android:layout_
                android:layout_marginTop="@dimen/keyline_5"
                android:src="@user.avatar"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/act_details_toolbar" />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/materialTextView"
                android:layout_
                android:layout_
                android:layout_marginStart="@dimen/keyline_4"
                android:layout_marginTop="@dimen/keyline_4"
                android:layout_marginEnd="@dimen/keyline_4"
                android:text="@user.name"
                android:textAlignment="center"
                android:textAppearance="?attr/textAppearanceHeadline2"
                android:textColor="?attr/colorOnBackground"
                app:layout_constraintTop_toBottomOf="@+id/shapeableImageView"
                tools:layout_editor_absoluteX="16dp"
                tools:text="april_ludgate" />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/materialTextView2"
                android:layout_
                android:layout_
                android:layout_marginStart="@dimen/keyline_4"
                android:layout_marginTop="@dimen/keyline_2"
                android:layout_marginEnd="@dimen/keyline_4"
                android:text="@`@` + user.username"
                android:textAlignment="center"
                android:textAppearance="?attr/textAppearanceSubtitle2"
                android:textColor="?attr/colorOnBackground"
                android:textSize="16sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/materialTextView"
                tools:text="April Ludgate" />

            <include
                android:id="@+id/act_details_details"
                layout="@layout/include_user_details"
                android:layout_
                android:layout_
                android:layout_marginStart="@dimen/keyline_4"
                android:layout_marginTop="@dimen/keyline_5"
                android:layout_marginEnd="@dimen/keyline_4"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/materialTextView2" />

            <com.google.android.material.tabs.TabLayout
                android:id="@+id/act_details_tab_layout"
                android:layout_
                android:layout_
                android:layout_marginTop="@dimen/keyline_5"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/act_details_details" />

            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/act_details_pager"
                android:layout_
                android:layout_
                android:clipChildren="false"
                android:clipToPadding="false"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/act_details_tab_layout" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</layout>

我在那里添加了另一个布局。这是我包含的布局文件 XML。

<?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_
    android:layout_
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/materialTextView2">

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/materialTextView3"
        android:layout_
        android:layout_
        android:drawableStart="@drawable/drawable_company"
        android:drawablePadding="@dimen/keyline_2"
        android:text="@string/company_label"
        android:textAlignment="center"
        android:textAppearance="?attr/textAppearanceBody1"
        android:textColor="?attr/colorOnBackground"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/materialTextView4"
        android:layout_
        android:layout_
        android:layout_marginTop="@dimen/keyline_0"
        android:drawableStart="@drawable/drawable_location"
        android:drawablePadding="@dimen/keyline_2"
        android:text="@string/location_label"
        android:textAlignment="center"
        android:textAppearance="?attr/textAppearanceBody1"
        android:textColor="?attr/colorOnBackground"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/materialTextView3" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/materialTextView5"
        android:layout_
        android:layout_
        android:layout_marginTop="@dimen/keyline_0"
        android:drawableStart="@drawable/drawable_repository"
        android:drawablePadding="@dimen/keyline_2"
        android:text="@string/repository_label"
        android:textAlignment="center"
        android:textAppearance="?attr/textAppearanceBody1"
        android:textColor="?attr/colorOnBackground"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/materialTextView4" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier2"
        android:layout_
        android:layout_
        app:barrierDirection="right"
        app:constraint_referenced_ids="materialTextView3,materialTextView4,materialTextView5" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/materialTextView8"
        android:layout_
        android:layout_
        android:layout_marginStart="@dimen/keyline_2"
        android:text="@`: ` + user.organizations"
        android:textAppearance="?attr/textAppearanceBody1"
        android:textColor="?attr/colorOnBackground"
        app:layout_constraintBottom_toBottomOf="@+id/materialTextView3"
        app:layout_constraintStart_toEndOf="@id/barrier2"
        app:layout_constraintTop_toTopOf="@+id/materialTextView3"
        tools:text=": Google, Inc." />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/materialTextView9"
        android:layout_
        android:layout_
        android:layout_marginStart="@dimen/keyline_2"
        android:text="@`: ` + user.location"
        android:textAppearance="?attr/textAppearanceBody1"
        android:textColor="?attr/colorOnBackground"
        app:layout_constraintBottom_toBottomOf="@+id/materialTextView4"
        app:layout_constraintStart_toEndOf="@id/barrier2"
        app:layout_constraintTop_toTopOf="@+id/materialTextView4"
        tools:text=": Pittsburgh, PA, U.S.A." />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/materialTextView10"
        formatNumberToTextWithColon="@1234"
        android:layout_
        android:layout_
        android:layout_marginStart="@dimen/keyline_2"
        android:textAppearance="?attr/textAppearanceBody1"
        android:textColor="?attr/colorOnBackground"
        app:layout_constraintBottom_toBottomOf="@+id/materialTextView5"
        app:layout_constraintStart_toEndOf="@id/barrier2"
        app:layout_constraintTop_toTopOf="@+id/materialTextView5"
        tools:text=": 1,234" />

</androidx.constraintlayout.widget.ConstraintLayout>

当我点击 Android Studio Preview 上的“i”图标时,我会收到这些消息。

The following classes could not be instantiated:
- androidx.constraintlayout.widget.Barrier (Open Class, Show Exception, Clear Cache)
- androidx.constraintlayout.widget.ConstraintLayout (Open Class, Show Exception, Clear Cache)
- com.google.android.material.tabs.TabLayout (Open Class, Show Exception, Clear Cache)
- com.google.android.material.appbar.MaterialToolbar (Open Class, Show Exception, Clear Cache)
Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE.  If this is an unexpected error you can also try to build the project, then manually refresh the layout.
Exception Details
java.lang.NoSuchFieldError: ConstraintLayout_Layout_barrierMargin
    at androidx.constraintlayout.widget.Barrier.init(Barrier.java:204)
    at androidx.constraintlayout.widget.ConstraintHelper.<init>(ConstraintHelper.java:85)
    at androidx.constraintlayout.widget.Barrier.<init>(Barrier.java:123)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:403)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:186)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:144)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:309)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:417)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:428)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:332)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1123)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
    at android.view.LayoutInflater_Delegate.parseInclude(LayoutInflater_Delegate.java:204)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1161)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1119)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1126)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:501)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:328)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:373)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:678)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$8(RenderTask.java:809)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

这是我的 build.gradle 文件中的依赖项。

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android 
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig 
        applicationId "com.mobile.githubuser"
        minSdkVersion 26
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    
    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        
    
    dataBinding 
        enabled = true
    
    android.sourceSets.all 
        java.srcDir('src/main/kotlin')
    


dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$rootProject.kotlinVersion"
    implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    implementation "androidx.core:core-ktx:$rootProject.coreKtxVersion"
    implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
    implementation "androidx.viewpager2:viewpager2:$rootProject.viewPagerVersion"

    implementation "com.google.android.material:material:$rootProject.mdcVersion"
    implementation "com.github.bumptech.glide:glide:$rootProject.glideVersion"

版本位于项目 gradle 文件中,可在此处查看。

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript 
    ext 
        kotlinVersion = '1.3.72'
        mdcVersion = '1.2.0-alpha03'
        constraintLayoutVersion = '2.0.0-beta1'
        appCompatVersion = '1.1.0-alpha03'
        coreKtxVersion = '1.3.0'
        viewPagerVersion = '1.0.0'
        glideVersion = '4.11.0'
    
    repositories 
        google()
        jcenter()
        
    
    dependencies 
        classpath 'com.android.tools.build:gradle:4.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    


allprojects 
    repositories 
        google()
        jcenter()
        
    


task clean(type: Delete) 
    delete rootProject.buildDir

我试过了。

同时安装适用于 Android 10.0 和 Android 8.1 的 SDK 我曾多次尝试使项目无效并重新启动 我已尝试多次清理和重建项目 我将我的 Android Studio IDE 更新到 4.0.1 更改我的 XML 中的主题(尽管这不是必需的)

我正在使用约束布局版本2.0.0-beta1 来使用 MotionLayout。以前,当我使用版本1.1.3(稳定版)时,预览效果很好,即它可以渲染ConstraintLayout。但是,即使我使用约束布局版本1.1.3,像MaterialToolbarShapeableImageView 这样的MDC 组件也总是无法渲染。我的依赖项中是否存在不兼容性?我该如何解决这个问题?

【问题讨论】:

您是否尝试过更改渲染引擎? SettingsExperimentalUse new Layout Rendering Engine @知道我有。新的实验布局渲染引擎和旧的我都试过了。 您是否尝试刷新布局或清理项目? @WahdatKashmiri 两者都是。 【参考方案1】:

首先,如果您将 ViewDataBiding 用于外部布局,那么您也可以将 ViewDataBinding 用于内部布局。

constarintLayoutVersion '2.0.0-beta1' 存在主要问题。如果您使用稳定版本 1.1.3 则可以使用。

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<data>
    <variable
        name="user"
        type="com.harman.uilearning.ViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.textview.MaterialTextView
    android:id="@+id/materialTextView3"
    android:layout_
    android:layout_
    android:drawableStart="@android:drawable/arrow_down_float"
    android:drawablePadding="10dp"
    android:text="Company Label"
    android:textAlignment="center"
    android:textAppearance="?attr/textAppearanceBody1"
    android:textColor="?attr/colorOnBackground"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<com.google.android.material.textview.MaterialTextView
    android:id="@+id/materialTextView4"
    android:layout_
    android:layout_
    android:layout_marginTop="10dp"
    android:drawableStart="@android:drawable/arrow_down_float"
    android:drawablePadding="10dp"
    android:text="location_label"
    android:textAlignment="center"
    android:textAppearance="?attr/textAppearanceBody1"
    android:textColor="?attr/colorOnBackground"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/materialTextView3"/>

<com.google.android.material.textview.MaterialTextView
    android:id="@+id/materialTextView5"
    android:layout_
    android:layout_
    android:layout_marginTop="10dp"
    android:drawableStart="@android:drawable/arrow_down_float"
    android:drawablePadding="10dp"
    android:text="repository_label"
    android:textAlignment="center"
    android:textAppearance="?attr/textAppearanceBody1"
    android:textColor="?attr/colorOnBackground"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/materialTextView4"/>

<androidx.constraintlayout.widget.Barrier
    android:id="@+id/barrier2"
    android:layout_
    android:layout_
    android:layout_margin="5dp"
    app:barrierDirection="right"
    app:constraint_referenced_ids="materialTextView3,materialTextView4,materialTextView5" />

<com.google.android.material.textview.MaterialTextView
    android:id="@+id/materialTextView8"
    android:layout_
    android:layout_
    android:layout_marginStart="10dp"
    android:text="user.organizations"
    android:textAppearance="?attr/textAppearanceBody1"
    android:textColor="?attr/colorOnBackground"
    app:layout_constraintBottom_toBottomOf="@+id/materialTextView3"
    app:layout_constraintStart_toEndOf="@id/barrier2"
    app:layout_constraintTop_toTopOf="@+id/materialTextView3"
    app:layout_constraintEnd_toEndOf="parent"
    tools:text=": Google, Inc." />

<com.google.android.material.textview.MaterialTextView
    android:id="@+id/materialTextView9"
    android:layout_
    android:layout_
    android:layout_marginStart="10dp"
    android:text="user.location"
    android:textAppearance="?attr/textAppearanceBody1"
    android:textColor="?attr/colorOnBackground"
    app:layout_constraintBottom_toBottomOf="@+id/materialTextView4"
    app:layout_constraintStart_toEndOf="@id/barrier2"
    app:layout_constraintTop_toTopOf="@+id/materialTextView4"
    app:layout_constraintEnd_toEndOf="parent"
    tools:text=": Pittsburgh, PA, U.S.A." />

<com.google.android.material.textview.MaterialTextView
    android:id="@+id/materialTextView10"
    android:layout_
    android:layout_
    android:layout_marginStart="10dp"
    android:textAppearance="?attr/textAppearanceBody1"
    android:textColor="?attr/colorOnBackground"
    app:layout_constraintBottom_toBottomOf="@+id/materialTextView5"
    app:layout_constraintStart_toEndOf="@id/barrier2"
    app:layout_constraintTop_toTopOf="@+id/materialTextView5"
    app:layout_constraintEnd_toEndOf="parent"
    tools:text=": 1,234" />

 </androidx.constraintlayout.widget.ConstraintLayout>
 </layout>

【讨论】:

我必须使用版本2.0.0-beta1,正如我的问题中所解释的那样才能使用 MotionLayout。 MotionLayout 在 1.1.3 中不可用。 您好,我添加了内部布局 xml,它几乎没有更改,只是将缺少的属性添加到您的布局并将约束布局版本更新为 2.0.0-beta3。随着这些变化。我可以看到预览 这很奇怪。我已将包含的布局修改为数据绑定布局,但预览仍然不适合我。此外,来自 MDC 的其他类也不起作用(例如 MaterialToolbar)。 你能解释一下你做得更好吗?【参考方案2】:

尝试使用 'def' 关键字声明,或者直接在依赖项中直接硬编码版本,我觉得 gradle 同步存在一些问题,

ext 
        def kotlinVersion = '1.3.72'
        def mdcVersion = '1.2.0-alpha03'
        def constraintLayoutVersion = '2.0.0-beta1'
        def appCompatVersion = '1.1.0-alpha03'
        def coreKtxVersion = '1.3.0'
        def viewPagerVersion = '1.0.0'
        def glideVersion = '4.11.0'
    

dependencies 
    ...
    implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta1"

【讨论】:

以上是关于ConstraintLayout 不能与 Android XML Preview 中的其他类一起实例化的主要内容,如果未能解决你的问题,请参考以下文章

markdown TextView不能在ConstraintLayout中使用wrap_content

HttpURLConnection 下载图片

Android学习蓝牙扫描

是否可以将 ConstraintLayout 放在 ScrollView 中?

Android ConstraintLayout 使用与适配(适配篇)

Android ConstraintLayout 使用与适配(适配篇)