Android AppBarLayout 预览异常

Android AppBarLayout preview exception

使用 AppBarLayout 时出错,我尝试了 Clean Project 和 Rebuild Project,但它们没有用。

程序没有问题运行。这是为什么?

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    </com.google.android.material.appbar.AppBarLayout>

</LinearLayout>

Gradle:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${kotlin_version}"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'android.arch.navigation:navigation-fragment:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    def nav_version = '1.0.0'
    implementation "android.arch.navigation:navigation-ui:$nav_version"
    implementation 'com.google.android.material:material:1.0.0'
}

堆栈跟踪:

java.lang.IllegalArgumentException: java.lang.ClassCastException@73c26ae
    at sun.reflect.GeneratedMethodAccessor274.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at android.animation.PropertyValuesHolder_Delegate.callMethod(PropertyValuesHolder_Delegate.java:108)
    at android.animation.PropertyValuesHolder_Delegate.nCallFloatMethod(PropertyValuesHolder_Delegate.java:143)
    at android.animation.PropertyValuesHolder.nCallFloatMethod(PropertyValuesHolder.java)
    at android.animation.PropertyValuesHolder.access0(PropertyValuesHolder.java:38)
    at android.animation.PropertyValuesHolder$FloatPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:1387)
    at android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:990)
    at android.animation.ValueAnimator.setCurrentFraction(ValueAnimator.java:674)
    at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:637)
    at android.animation.ValueAnimator.start(ValueAnimator.java:1069)
    at android.animation.ValueAnimator.start(ValueAnimator.java:1088)
    at android.animation.ObjectAnimator.start(ObjectAnimator.java:852)
    at android.animation.StateListAnimator.start(StateListAnimator.java:188)
    at android.animation.StateListAnimator.setState(StateListAnimator.java:181)
    at android.view.View.drawableStateChanged(View.java:21105)
    at android.view.ViewGroup.drawableStateChanged(ViewGroup.java:7101)
    at android.view.View.refreshDrawableState(View.java:21160)
    at android.view.View.dispatchAttachedToWindow(View.java:18379)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3397)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404)
    at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:42)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:335)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:391)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:195)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:540)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:666)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

描述不够可以发布吗?描述不够可以发布吗?

你需要这样使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

    </com.google.android.material.appbar.AppBarLayout>

</LinearLayout>

因为您在 gradle 中添加了 Material 依赖项 作为 com.google.android.material

我不知道它是怎么导致的。

重启电脑就解决了

我在 Linux(AS3.4 和 AS3.5 - canary)中尝试了一些使用完全相同代码的 AppBarLayout 项目,在 Windows 我在 AS3 中尝试过。 3.支持库中的 AppBarLayout 似乎有些不对。我用的是androidx。几次成功构建后似乎出了点问题。

这本身不是解决方案,而是一种可能的解决方法 - 在更新库并且 AppBarLayout 表现良好之前,您可以避免使用 AppBarLayout。

我遇到了同样的问题,因为我在迁移到 androidX 后将我的 material 库升级到了 1.1.0-alpha10。

作为解决方法,我不得不降级到 'com.google.android.material:material:1.0.0',然后重新启动 android studio。 希望能帮助到你。