由于 Android Studio 中的折叠工具栏导致的渲染问题

Posted

技术标签:

【中文标题】由于 Android Studio 中的折叠工具栏导致的渲染问题【英文标题】:Render problem due to Collapsing Toolbar in Android Studio 【发布时间】:2021-06-15 21:11:57 【问题描述】:

我的布局编辑器不会使用折叠工具栏渲染此布局,其他布局渲染得很好。我尝试过刷新、使缓存/重新启动无效、将 gradle 实现设置为稳定版本、将 API 29 和 28 设置为预览而不是 30、将项目与 gradle 文件同步、切换到新的/实验性渲染引擎。到目前为止没有任何效果。该问题似乎是由于折叠工具栏造成的,如下面的堆栈跟踪所示。

java.lang.NullPointerException
    at android.view.ViewGroup.brokenDispatchApplyWindowInsets(ViewGroup.java:7200)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7193)
    at android.view.ViewGroup.brokenDispatchApplyWindowInsets(ViewGroup.java:7203)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7193)
    at android.view.ViewGroup.brokenDispatchApplyWindowInsets(ViewGroup.java:7203)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7193)
    at android.view.ViewGroup.brokenDispatchApplyWindowInsets(ViewGroup.java:7203)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7193)
    at android.view.ViewGroup.brokenDispatchApplyWindowInsets(ViewGroup.java:7203)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7193)
    at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:1925)
    at android.view.ViewRootImpl_Accessor.dispatchApplyInsets(ViewRootImpl_Accessor.java:24)
    at com.android.layoutlib.bridge.impl.Layout.requestFitSystemWindows(Layout.java:385)
    at android.view.View.requestFitSystemWindows(View.java:10796)
    at android.view.View.requestFitSystemWindows(View.java:10796)
    at android.view.View.requestFitSystemWindows(View.java:10796)
    at android.view.View.requestFitSystemWindows(View.java:10796)
    at android.view.View.requestApplyInsets(View.java:10804)
    at androidx.core.view.ViewCompat.requestApplyInsets(ViewCompat.java:2426)
    at androidx.core.view.ViewCompat$Api21Impl$1.onApplyWindowInsets(ViewCompat.java:4774)
    at android.view.View.dispatchApplyWindowInsets(View.java:10514)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7191)
    at android.view.ViewGroup.brokenDispatchApplyWindowInsets(ViewGroup.java:7203)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7193)
    at android.view.ViewGroup.brokenDispatchApplyWindowInsets(ViewGroup.java:7203)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7193)
    at android.view.ViewGroup.brokenDispatchApplyWindowInsets(ViewGroup.java:7203)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7193)
    at android.view.ViewGroup.brokenDispatchApplyWindowInsets(ViewGroup.java:7203)
    at android.view.ViewGroup.dispatchApplyWindowInsets(ViewGroup.java:7193)
    at android.view.ViewRootImpl.dispatchApplyInsets(ViewRootImpl.java:1925)
    at android.view.ViewRootImpl_Accessor.dispatchApplyInsets(ViewRootImpl_Accessor.java:24)
    at com.android.layoutlib.bridge.impl.Layout.requestFitSystemWindows(Layout.java:385)
    at android.view.View.requestFitSystemWindows(View.java:10796)
    at android.view.View.requestFitSystemWindows(View.java:10796)
    at android.view.View.requestFitSystemWindows(View.java:10796)
    at android.view.View.requestFitSystemWindows(View.java:10796)
    at android.view.View.requestFitSystemWindows(View.java:10796)
    at android.view.View.requestApplyInsets(View.java:10804)
    at androidx.core.view.ViewCompat.requestApplyInsets(ViewCompat.java:2426)
    at com.google.android.material.appbar.CollapsingToolbarLayout.onAttachedToWindow(CollapsingToolbarLayout.java:273)
    at android.view.View.dispatchAttachedToWindow(View.java:19575)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3430)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
    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:373)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:713)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$6(RenderTask.java:844)
    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)

导致问题的布局


<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/coordinatorLayout"
    android:layout_
    android:layout_
    android:background="@android:color/background_light">
    <!--    android:fitsSystemWindows="true"-->


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_
        android:layout_
        android:background="@android:color/white"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <!--            android:fitsSystemWindows="true"-->

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_
            android:layout_
            app:collapsedTitleGravity="center"
            app:collapsedTitleTextAppearance="@style/collapsedBlackTitle"
            app:contentScrim="@color/white"
            app:expandedTitleGravity="center"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleTextAppearance="@style/expandedWhiteTitle"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            .
            .
            .

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

        <!--- ADD TAB_LAYOUT HERE -->
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayoutMedia"
            android:layout_
            android:layout_
            android:layout_marginTop="5dp"
            app:layout_constraintBottom_toTopOf="@id/recycleView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view5"
            app:tabIconTint="@color/selected_orange_item">

            .
            .
            .
            
        </com.google.android.material.tabs.TabLayout>

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

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_
        android:layout_
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/postsConstraintLayout"
            android:layout_
            android:layout_
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_
                android:layout_
                android:orientation="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"

                />

            <TextView
                android:id="@+id/tvNoPosts"
                android:layout_
                android:layout_
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="@dimen/_16sdp"
                android:gravity="center"
                android:text="No Posts"
                android:textSize="20sp"
                android:visibility="visible"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.1" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


    <androidx.cardview.widget.CardView
        android:id="@+id/cardView3"
        android:layout_
        android:layout_
        android:clickable="true"
        android:focusable="true"
        android:foreground="?android:attr/selectableItemBackground"
        android:visibility="gone"
        app:cardBackgroundColor="@color/colorPrimary"
        app:cardCornerRadius="30dp"
        app:cardElevation="10dp"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|center_horizontal|clip_horizontal"
        app:layout_constraintEnd_toEndOf="@+id/imageView16"
        app:layout_constraintStart_toStartOf="@+id/imageView16"
        app:layout_constraintTop_toBottomOf="@+id/imageView16">

        <TextView
            android:layout_
            android:layout_
            android:gravity="center"
            android:paddingHorizontal="70dp"
            android:paddingVertical="15dp"
            android:text="Play All"
            android:textAllCaps="true"
            android:textColor="@color/white"
            android:textSize="22sp"
            android:textStyle="bold" />
    </androidx.cardview.widget.CardView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

相关的 gradle 实现

    implementation 'com.google.android.material:material:1.3.0'

【问题讨论】:

【参考方案1】:

转到文件 -> 项目结构 -> 建议 -> 找到所有相关库并更新到最稳定的版本,在我的例子中是 appcompat。中提琴!渲染引擎能够渲染布局,尽管它显示 Path.op() 是另一个问题,但它可以工作。

【讨论】:

以上是关于由于 Android Studio 中的折叠工具栏导致的渲染问题的主要内容,如果未能解决你的问题,请参考以下文章

在 Android Studio 中使用 //region 折叠错误

带有折叠工具栏的 RecyclerView 中的 Android 字母快速滚动视图

它们是 Android Studio 的“类似区域”的折叠功能吗

Android 中的折叠底栏(如 Google 音乐)

如何防止 Android Studio 在项目导航中自动折叠我的包?

工具栏折叠时显示视图