android-maps-compose icon indicating copy to clipboard operation
android-maps-compose copied to clipboard

Preview of MarkerComposable doesn't work

Open barry-irvine opened this issue 9 months ago • 1 comments

Please be sure to include as much information as possible:

Environment details

  1. Maps Compose 6.5.2
  2. Compose 1.7.8
  3. Android Studio Meerkat 2024.3.1 Patch 1

Steps to reproduce

  1. Create a simple Marker Composable function and then create a preview of it

Code example

@Preview
@Composable
fun PreviewNumberedMarker() {
    NumberedMarker(1, rememberUpdatedMarkerState(), true) { }
}

@Composable
fun NumberedMarker(
    number: Int,
    state: MarkerState = rememberUpdatedMarkerState(),
    isActive: Boolean,
    onClick: () -> Unit
) {
    MarkerComposable(
        keys = arrayOf(number, isActive),
        onClick = { onClick(); true },
        state = state,
        zIndex = if (isActive) 1f else 0f
    ) {
        Surface(
            color = if (isActive) DarkTeal else Color.White,
            contentColor = if (isActive) Color.White else DeepBlue,
            shape = CircleShape,
        ) {
            Box(
                modifier = Modifier.size(48.dp),
                contentAlignment = Alignment.Center
            ) {
                Text(number.toString(), style = MaterialTheme.typography.labelSmall)
            }
        }
    }
}

Stack trace

This preview was unable to find a CompositionLocal. You might need to define it so it can render correctly.

java.lang.IllegalStateException: The ComposeView was measured to have a width or height of zero. Make sure that the content has a non-zero size.
	at com.google.maps.android.compose.RememberComposeBitmapDescriptorKt.renderComposableToBitmapDescriptor(RememberComposeBitmapDescriptor.kt:58)
	at com.google.maps.android.compose.RememberComposeBitmapDescriptorKt.access$renderComposableToBitmapDescriptor(RememberComposeBitmapDescriptor.kt:1)
	at com.google.maps.android.compose.RememberComposeBitmapDescriptorKt.rememberComposeBitmapDescriptor(RememberComposeBitmapDescriptor.kt:29)
	at com.google.maps.android.compose.MarkerKt.MarkerComposable-Khg_OnI(Marker.kt:342)
	at com.gocitypass.design.maps.NumberedMarkerKt.NumberedMarker(NumberedMarker.kt:34)
	at com.gocitypass.design.maps.NumberedMarkerKt.PreviewNumberedMarker(NumberedMarker.kt:24)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
	at androidx.compose.ui.tooling.ComposableInvoker.invokeComposableMethod(ComposableInvoker.jvm.kt:181)
	at androidx.compose.ui.tooling.ComposableInvoker.invokeComposable(ComposableInvoker.jvm.kt:221)
	at androidx.compose.ui.tooling.ComposeViewAdapter$init$3$1$composable$1$1.invoke(ComposeViewAdapter.android.kt:504)
	at androidx.compose.ui.tooling.ComposeViewAdapter$init$3$1$composable$1$1.invoke(ComposeViewAdapter.android.kt:502)
	at androidx.compose.ui.tooling.ComposeViewAdapter$init$3$1.invoke(ComposeViewAdapter.android.kt:539)
	at androidx.compose.ui.tooling.ComposeViewAdapter$init$3$1.invoke(ComposeViewAdapter.android.kt:497)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
	at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:380)
	at androidx.compose.ui.tooling.InspectableKt.Inspectable(Inspectable.android.kt:61)
	at androidx.compose.ui.tooling.ComposeViewAdapter$WrapPreview$1.invoke(ComposeViewAdapter.android.kt:444)
	at androidx.compose.ui.tooling.ComposeViewAdapter$WrapPreview$1.invoke(ComposeViewAdapter.android.kt:443)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
	at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:380)
	at androidx.compose.ui.tooling.ComposeViewAdapter.WrapPreview(ComposeViewAdapter.android.kt:438)
	at androidx.compose.ui.tooling.ComposeViewAdapter.access$WrapPreview(ComposeViewAdapter.android.kt:124)
	at androidx.compose.ui.tooling.ComposeViewAdapter$init$3.invoke(ComposeViewAdapter.android.kt:497)
	at androidx.compose.ui.tooling.ComposeViewAdapter$init$3.invoke(ComposeViewAdapter.android.kt:494)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
	at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:441)
	at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:259)
	at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:258)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
	at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:380)
	at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:216)
	at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:132)
	at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:131)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
	at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:380)
	at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:121)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:155)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:154)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
	at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:401)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:154)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:133)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
	at androidx.compose.runtime.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:97)
	at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3595)
	at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:3522)
	at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:743)
	at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:1122)
	at androidx.compose.runtime.CompositionImpl.composeInitial(Composition.kt:649)
	at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:635)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:133)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:124)
	at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:1626)
	at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:124)
	at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:180)
	at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.jvm.kt:320)
	at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.jvm.kt:198)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:131)
	at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:124)
	at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:1707)
	at android.view.View.dispatchAttachedToWindow(View.java:22895)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3506)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3513)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3513)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3513)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3513)
	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3513)
	at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:69)
	at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:361)
	at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:462)
	at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:125)
	at com.android.tools.rendering.RenderTask.createRenderSession(RenderTask.java:782)
	at com.android.tools.rendering.RenderTask.lambda$inflate$6(RenderTask.java:930)
	at com.android.tools.rendering.RenderExecutor.runAsyncActionWithTimeout$lambda$12(RenderExecutor.kt:210)
	at com.android.tools.rendering.RenderExecutor$PriorityRunnable.run(RenderExecutor.kt:327)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)


Following these steps will guarantee the quickest resolution possible.

Thanks!

barry-irvine avatar Apr 08 '25 07:04 barry-irvine

Hi @barry-irvine ,

AndroidViews are not supported by Preview (we also have the topic of the map also not being able to render, due to the key usage).

One possibility would be to provide a mechanism similar to the one implemented in Accompanist, eventually returning a simple AndroidView (so it doesn't render, but it does not break) or maybe rendering and returning the content() Composable function?

kikoso avatar Apr 08 '25 12:04 kikoso