android - ConstraintLayout 不能与 Android XML Preview 中的其他类一起实例化
问题描述
有些类没有在我的 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_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/act_details_toolbar"
android:layout_width="0dp"
android:layout_height="@dimen/toolbar_height"
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_width="@dimen/user_details_avatar_size"
android:layout_height="@dimen/user_details_avatar_size"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="0dp"
android:layout_height="wrap_content"
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_width="0dp"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="materialTextView3,materialTextView4,materialTextView5" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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 预览版上的“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
. 但是,即使我使用约束布局版本,MDC 组件MaterialToolbar
也ShapeableImageView
总是无法渲染1.1.3
。我的依赖项中是否存在不兼容性?我怎样才能解决这个问题?
解决方案
首先,如果您将 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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
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>
推荐阅读
- sql - 如何强制 LEFT OUTER JOIN 返回 NULL 或空?
- html - 如何使用 nth-child 更改每隔一个 wordpress 帖子的位置?
- python - 使用 python3 发送一封简单的电子邮件
- rust - 如何从常量函数内部打印中间值以进行调试?
- python-3.x - Apache Kafka Python - self.async
- webpack - 将 postcss-loader 从 3.0.0 更新到 4.1.0 后 webpack.code 构建被破坏
- php - 如何阻止 PHP include() 函数重定向?
- .net-core - PrivateAssets='All' 是什么意思?
- python - 获取 numpy 二维数组中与零相邻的所有元素的索引
- c - 远程本地任务栏