java.lang.NoSuchFieldError: No field Companion of type Landroidx/compose/foundation/layout/BoxScope$Companion;

java.lang.NoSuchFieldError: No field Companion of type Landroidx/compose/foundation/layout/BoxScope$Companion;

我是第一次使用 Jetpack Compose,但遇到此错误。我还没有弄清楚问题到底出在哪里,但我使用的是单activity架构。如果需要更多信息,请通知我。 根据报错,问题似乎出在脚手架上。

                val scaffoldState = rememberScaffoldState()

                Scaffold(
                    scaffoldState = scaffoldState,
                    snackbarHost = {
                        SnackbarHost(hostState = it)
                    }
                ) {
java.lang.NoSuchFieldError: No field Companion of type Landroidx/compose/foundation/layout/BoxScope$Companion; in class Landroidx/compose/foundation/layout/BoxScope; or its superclasses (declaration of 'androidx.compose.foundation.layout.BoxScope' appears in /data/app/com.octagon_technologies.scafe-0B8-dDpbnRqa6fydxFPekw==/base.apk)
        at androidx.compose.material.SurfaceKt$Surface.invoke(Surface.kt:149)
        at androidx.compose.material.SurfaceKt$Surface.invoke(Surface.kt:105)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
        at androidx.compose.material.SurfaceKt.Surface-F-jzlyU(Surface.kt:102)
        at androidx.compose.material.ScaffoldKt$Scaffold$child.invoke(Scaffold.kt:168)
        at androidx.compose.material.ScaffoldKt$Scaffold$child.invoke(Scaffold.kt:167)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:118)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.material.ScaffoldKt.Scaffold-J67Y1T8(Scaffold.kt:197)
        at com.octagon_technologies.scafe.presentation.MainActivity$onCreate.invoke(MainActivity.kt:54)
        at com.octagon_technologies.scafe.presentation.MainActivity$onCreate.invoke(MainActivity.kt:47)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
        at androidx.compose.material.TextKt.ProvideTextStyle(Text.kt:246)
        at androidx.compose.material.MaterialThemeKt$MaterialTheme.invoke(MaterialTheme.kt:78)
        at androidx.compose.material.MaterialThemeKt$MaterialTheme.invoke(MaterialTheme.kt:77)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
        at androidx.compose.material.MaterialThemeKt.MaterialTheme(MaterialTheme.kt:69)
        at com.octagon_technologies.scafe.presentation.ui.theme.ThemeKt.ScafeTheme(Theme.kt:46)
        at com.octagon_technologies.scafe.presentation.MainActivity$onCreate.invoke(MainActivity.kt:47)
        at com.octagon_technologies.scafe.presentation.MainActivity$onCreate.invoke(MainActivity.kt:46)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:346)
        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated.invoke(ComposeView.android.kt:202)
        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated.invoke(ComposeView.android.kt:201)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)

这段代码我遇到了同样的错误

java.lang.NoSuchFieldError: 
No static field Companion of type Landroidx/compose/foundation/layout/BoxScope$Companion; 
in class Landroidx/compose/foundation/layout/BoxScope; 
or its superclasses (declaration of 'androidx.compose.foundation.layout.BoxScope' 
appears in /data/app/ink.iamt.demo-l1DoSMoQUFGcF5KYI5RU0w==/base.apk)
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Card {
                Text("Hello, Compose")
            }
        }
    }
}

你知道我的代码再简单不过了。所以我想这不应该是我的错。 我检查了我的 Android Studio 版本,它是 2020.3.1 Canary 14,这是最新版本。并检查了 Jetpack compose 的 build.gradle,它是

    val compose_version by extra("1.0.0-beta01")

所以最新的 Android Studio 创建了一个新的 Compose 项目,compose 版本是 1.0.0-beta01,而最新的 compose 版本是 1.0.0-beta04。然后我尝试将版本更新为 beta04 并重新运行我的应用程序。然后就可以了!

所以请尝试将您的撰写版本更新到最新版本,看看您是否有运气。

添加 androidx.navigation:navigation-compose:1.0.0-alpha10 作为依赖项后出现此错误。

对我有用的是降级到 androidx.navigation:navigation-compose:1.0.0-alpha09

我遇到了同样的错误,通过更新compose版本解决了。 AndroidStudio生成的新项目模板自带compose版本1.0.0-beta01

打开 build.gradle 文件并更新

compose_version = '1.0.0-beta05'

或更新版本(如果您愿意)。

此答案基于帮助我修复错误的 CommonsWare 评论。

解决方案是使用一组一致的 Compose 依赖项。 beta04 对 compose-foundation 工件中的 BoxScope 进行了更改。也许您获得的是该工件的 beta04,但获得的是较旧的 compose-material 工件。