Android Compose无法在android studio“渲染问题”中预览可组合功能

时间:2021-01-13 09:51:25

标签: android android-jetpack-compose

我无法预览下面代码中显示的可组合函数

@Preview
@Composable
fun StartingConversationInstructions() {
    Column {
        Text(stringResource(id = R.string.remember))

    }
}

但是当我尝试预览函数时出现此错误

java.lang.NoSuchMethodError: 'androidx.compose.runtime.SlotTable
androidx.compose.runtime.Composer.getSlotTable()'   at 
androidx.ui.tooling.InspectableKt.Inspectable(Inspectable.kt:63)   at 
androidx.ui.tooling.preview.ComposeViewAdapter$WrapPreview$1.invoke(ComposeViewAdapter.kt:357)   at
 androidx.ui.tooling.preview.ComposeViewAdapter$WrapPreview$1.invoke(ComposeViewAdapter.kt:-1)   at
 androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)   at 
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:-1)   at 
androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188)   at 
androidx.ui.tooling.preview.ComposeViewAdapter.WrapPreview(ComposeViewAdapter.kt:356)   at 
androidx.ui.tooling.preview.ComposeViewAdapter.access$WrapPreview(ComposeViewAdapter.kt:-1)   at 
androidx.ui.tooling.preview.ComposeViewAdapter$init$1.invoke(ComposeViewAdapter.kt:397)   at 
androidx.ui.tooling.preview.ComposeViewAdapter$init$1.invoke(ComposeViewAdapter.kt:-1)   at 
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)   at 
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:-1)   at 
androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188)   at 
androidx.compose.ui.platform.AmbientsKt.ProvideCommonAmbients(Ambients.kt:274)   at 
androidx.compose.ui.platform.AndroidAmbientsKt$ProvideAndroidAmbients$3.invoke(AndroidAmbients.kt:176)   at 
androidx.compose.ui.platform.AndroidAmbientsKt$ProvideAndroidAmbients$3.invoke(AndroidAmbients.kt:-1)   at androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)   at 
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:-1)   at 
androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188)   at 
androidx.compose.ui.platform.AndroidAmbientsKt.ProvideAndroidAmbients(AndroidAmbients.kt:168)   at 
androidx.compose.ui.platform.WrappedComposition$setContent$1$1$2.invoke(Wrapper.kt:251)   at 
androidx.compose.ui.platform.WrappedComposition$setContent$1$1$2.invoke(Wrapper.kt:-1)   at 
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)   at 
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:-1)   at 
androidx.compose.runtime.AmbientKt.Providers(Ambient.kt:188)   at 
androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.kt:250)   at 
androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.kt:-1)   at 
.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:146)   at 
androidx.compose.runtime.internal.ComposableLambda.invoke(ComposableLambda.kt:-1)   at 
androidx.compose.runtime.ComposerKt.invokeComposable(Composer.kt:2904)   at 
androidx.compose.runtime.Composer.composeInitial(Composer.kt:2070)   at 
androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:353)   at 
androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:109)   at 
androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.kt:235)   at 
androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.kt:-1)   at 
androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.kt:545) 
  at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.kt:226)   at 
androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.kt:275)   at 
androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)   at 
androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:196)   at 
androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.kt:233)   at 
androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.kt:-1)   at 
androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.kt:610)   at 
android.view.View.dispatchAttachedToWindow(View.java:20479)   at 
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489)   at 
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)   at 
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)   at 
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)   at 
android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)   at 
android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:44) Copy stack to clipboard  
 Tip: Try to refresh the layout. 



我使用的是 android studio 版本:Android Studio 北极狐 | 2020.3.1 金丝雀 4
和 Android Compose 版本:1.0.0-alpha09

我认为这是导致问题的原因,因为您可以在此版本的 changes

中找到 <块引用>

从公共 API 中删除了 SlotTable、SlotReader 和 SlotWriter。这些之前被标记为 InternalComposeAPI。现在它们位于 compose 模块的内部。

2 个答案:

答案 0 :(得分:5)

在您的依赖项中,更改

implementation "androidx.ui:ui-tooling:$compose_version"

implementation "androidx.compose.ui:ui-tooling:$compose_version"

那行得通吗?

答案 1 :(得分:0)

正如@author 在他的 comment 中所述,有两个相同的注释:

import androidx.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.Preview

第二个对我有用。