当向选项卡布局中添加选项卡项时,Android Studio预览停止工作。

3

我注意到预览在许多电脑上都崩溃了,而不仅仅是我的电脑。但我目前遇到的最后一个问题是,在 TabLayout 中添加 TabItem 时,我找不到任何解决方案。我尝试了以下操作:

  • 清除项目,重新构建项目
  • 无效缓存并重新启动
  • 重新启动我的笔记本电脑
  • 检查该布局中是否有任何错误(没有错误,只有关于提取字符串的警告)
  • 检查材料组件版本(我使用的是当前最新版本1.8.0)

也许是我忘记了一些愚蠢的东西,但现在我想不出来了。这是我的 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="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.AdminActivity">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/adminTab"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorColor="@color/befc_blue"
        app:tabSelectedTextColor="@color/befc_blue">


        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/chart"
            android:text="Reports" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/croissant"
            android:text="Hot Potatoes" />
    </com.google.android.material.tabs.TabLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

这是我得到的错误信息:
错误信息

First Error

Second Error

Third Error

我尝试将默认的Material Design样式添加到选项卡布局中,但没有成功


@deadfish 什么错误日志? - Charbel983
@deadfish,我编辑了我的帖子,你可以看到我遇到了哪些错误。 - Charbel983
textAppearanceTitleSmall - 你在哪里使用它? - deadfish
你正在使用新的设计吗?https://material.io/blog/migrating-material-3 - deadfish
@deadfish 是的,我找到了一个解决方案,只需要在程序中进行操作。我在我的 XML 中放置了一个空的 TabLayout,并在我的 Kotlin 代码中添加了 TabItems。 - Charbel983
显示剩余2条评论
1个回答

2

我也遇到了这个问题,似乎这是一个已知的问题,建议降级到1.17.0版本。TabLayout和ViewHolder的渲染问题

我找到了一个解决方法,虽然不是最好的解决方案,但我能够通过从XML中删除选项卡,然后在程序中添加它们来解决预览崩溃的问题。

 tabLayout.addTab(tabLayout.newTab().setText("Tab1"))
 tabLayout.addTab(tabLayout.newTab().setText("Tab2"))

这使我仍然可以看到我的预览的其余部分,解除了我的阻塞。我还认为Android Studio更注重于Compose布局修复而不是XML。因此不确定是否会得到修复。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接