androidx.compose.ui.tooling.preview.PreviewActivity 不是活动子类或别名

时间:2021-07-23 12:15:08

标签: android android-jetpack-compose

我将 compose 和 kotlinCompilerExtensionVersion 从 1.0.0-rc02 更新为 beta09,Kotlin 版本更新为 1.5.21,Gradle 版本更新为 7.1,Gradle 插件版本更新为 7.1.0-alpha04< /p>

从那时起,我在尝试运行 androidx.compose.ui.tooling.preview.PreviewActivity is not an activity subclass or alias 时遇到此错误 MyScreen - 我无法解决。 我该如何解决?

5 个答案:

答案 0 :(得分:3)

使用 Compose 1.0,Kotlin 1.5.10 和 Android Studio 2020.3.1 在大多数预览中仍然存在相同的问题(有些没有)

在这种情况下,在“编辑配置”的顶部栏菜单中帮助手动创建新配置,其参数与无效配置中的参数相同并带有警告(“androidx.compose.ui.tooling.PreviewActivity 不是 Activity 子类或别名”)。这种新的手动创建的配置对我有用。

答案 1 :(得分:1)

我在 Compose 1.0.0 和 Kotlin 1.5.10(推荐)上遇到了同样的错误。

enter image description here

我通过在我的 gradle 文件中添加以下依赖项 debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" 修复了这些问题。

不要忘记将依赖的版本设置为您当前的撰写版本。

enter image description here

重建并重新加载预览后,一切正常。

示例预览如下所示:

package com.example.compose.basics.ui

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import de.db.dbs.travio.compose.basics.themeing.ComposeBasicsTheme

@Preview
@Composable
fun ScreenContent() {
    ComposeBasicsTheme {
        Card(modifier = Modifier
                .padding(8.dp)
                .shadow(8.dp, RoundedCornerShape(32.dp))
        ) {
            Text(
                    text = "Hello World!",
                    modifier = Modifier.padding(16.dp)
            )
        }
    }
}

答案 2 :(得分:0)

对我有用的是将 ui-tooling 版本降级到 beta09,即将这个依赖项用于 ui-tooling:

implementation "androidx.compose.ui:ui-tooling:1.0.0-beta09"

答案 3 :(得分:0)

如果使用的版本低于此处提到的版本,则将 compose_version 更新为 1.0.1 并将 kotlin-gradle-plugin 更新为 1.5.21 可解决此问题。

答案 4 :(得分:0)

我在使用 Bumblebee(又名,2021.1.1)Canary 6、7 和 8 时遇到此错误,使用 compose 版本 1.0.0、1.0.1 和 1.1.0-alpha01。我刚刚在 Studio 问题跟踪器上提出了一个错误:

https://issuetracker.google.com/issues/196248459

引用我自己的话:

<块引用>

很明显这是一个“左手不知道右手在做什么”的事情,因为 PreviewActivity 不在那个包中,而是 androidx.compose.ui.tooling – 它没有在android.compose.ui.tooling.preview 自 compose 1.0.0-beta09.