Actual/expect funs in shared throw NoSuchMethodError in Kotlin Multiplatform - android

I am developing a dummy weather app with the help of Kotlin Multiplatform. I am using Decompose and MVIKotlin to try and share all the business logic except the native uis, SwiftUI for IOS and Compose for Desktop and Android.
I am trying to have a expect composable (for dark theme and for loading of images)
// In common main
#Composable
expect fun isDarkMode() : Boolean
#Composable
expect fun loadWeatherIcon(id : Int) : Painter
// In android main
#Composable
actual fun isDarkMode() : Boolean {
return androidx.compose.foundation.isSystemInDarkTheme()
}
#Composable
actual fun loadWeatherIcon(id : Int) : Painter {
return painterResource(id)
}
//In Desktop main
#Composable
actual fun isDarkMode() : Boolean {
return false //isSystemInDarkTheme()
}
#Composable
actual fun loadWeatherIcon(id : Int) : Painter {
return loadWeatherIcon(id)
}
Meaning they are all implemented and there, but for some reason the crash is happening with NoSuchMethodError. I found some similar issues but they all had suspend funs and default parameters. Meanwhile my approach should work like in the jetpack todo compose sample, here's my project (if you want to test what's happening, you can just clone it and run it, it should crash immediatelly)
https://github.com/Nikola-Milovic/KotlinMultiplaftorm-WeatherApp
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nikolam.kmm_weather.androidApp, PID: 17270
java.lang.NoSuchMethodError: No static method isDarkMode(Landroidx/compose/runtime/Composer;I)Z in class Lcom/nikolam/kmm_weather/ui/UtilsKt; or its super classes (declaration of 'com.nikolam.kmm_weather.ui.UtilsKt' appears in /data/data/com.nikolam.kmm_weather.androidApp/code_cache/.overlay/base.apk/classes4.dex)
at com.nikolam.kmm_weather.ui.WeatherMainContentKt$WeatherMainContent$2.invoke(WeatherMainContent.kt:45)
at com.nikolam.kmm_weather.ui.WeatherMainContentKt$WeatherMainContent$2.invoke(WeatherMainContent.kt:38)
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 com.nikolam.kmm_weather.ui.WeatherMainContentKt.WeatherMainContent(WeatherMainContent.kt:38)
at com.nikolam.kmm_weather.ui.ComposableSingletons$WeatherRootContentKt$lambda-1$1.invoke(WeatherRootContent.kt:14)
at com.nikolam.kmm_weather.ui.ComposableSingletons$WeatherRootContentKt$lambda-1$1.invoke(WeatherRootContent.kt:12)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:118)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at com.arkivanov.decompose.extensions.compose.jetbrains.ChildrenKt$Children$1$1.invoke(Children.kt:30)
at com.arkivanov.decompose.extensions.compose.jetbrains.ChildrenKt$Children$1$1.invoke(Children.kt:29)
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.runtime.saveable.SaveableStateHolderImpl.SaveableStateProvider(SaveableStateHolder.kt:84)
at com.arkivanov.decompose.extensions.compose.jetbrains.ChildrenKt$Children$1.invoke(Children.kt:29)
at com.arkivanov.decompose.extensions.compose.jetbrains.ChildrenKt$Children$1.invoke(Children.kt:28)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:118)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at com.arkivanov.decompose.extensions.compose.jetbrains.animation.page.PageAnimationKt$PageAnimationFrame$1$1$1.invoke(PageAnimation.kt:143)
at com.arkivanov.decompose.extensions.compose.jetbrains.animation.page.PageAnimationKt$PageAnimationFrame$1$1$1.invoke(PageAnimation.kt:141)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:118)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at com.arkivanov.decompose.extensions.compose.jetbrains.animation.child.CrossfadeScaleKt$crossfadeScale$1.invoke(CrossfadeScale.kt:22)
at com.arkivanov.decompose.extensions.compose.jetbrains.animation.child.CrossfadeScaleKt$crossfadeScale$1.invoke(CrossfadeScale.kt:21)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:254)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at com.arkivanov.decompose.extensions.compose.jetbrains.animation.page.PageAnimationKt$PageAnimationFrame$1.invoke(PageAnimation.kt:141)
at com.arkivanov.decompose.extensions.compose.jetbrains.animation.page.PageAnimationKt$PageAnimationFrame$1.invoke(PageAnimation.kt:132)
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.foundation.layout.BoxWithConstraintsKt$BoxWithConstraints$1$1$measurables$1.invoke(BoxWithConstraints.kt:61)
E/AndroidRuntime: at androidx.compose.foundation.layout.BoxWithConstraintsKt$BoxWithConstraints$1$1$measurables$1.invoke(BoxWithConstraints.kt:61)
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.layout.SubcomposeLayoutState$subcompose$2$1.invoke(SubcomposeLayout.kt:167)
at androidx.compose.ui.layout.SubcomposeLayoutState$subcompose$2$1.invoke(SubcomposeLayout.kt:167)
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.ComposerKt.invokeComposable(Composer.kt:3235)
at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2514)
at androidx.compose.runtime.ComposerImpl.composeContent$runtime_debug(Composer.kt:2478)
at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:453)
at androidx.compose.runtime.Recomposer.composeInitial$runtime_debug(Recomposer.kt:702)
at androidx.compose.runtime.ComposerImpl$CompositionContextImpl.composeInitial$runtime_debug(Composer.kt:2914)
at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:409)
at androidx.compose.ui.layout.SubcomposeLayoutState.subcomposeInto(SubcomposeLayout.kt:184)
at androidx.compose.ui.layout.SubcomposeLayoutState.access$subcomposeInto(SubcomposeLayout.kt:100)
at androidx.compose.ui.layout.SubcomposeLayoutState$subcompose$2.invoke(SubcomposeLayout.kt:160)
at androidx.compose.ui.layout.SubcomposeLayoutState$subcompose$2.invoke(SubcomposeLayout.kt:158)
at androidx.compose.runtime.snapshots.SnapshotStateObserver.withNoObservations(SnapshotStateObserver.kt:137)
at androidx.compose.ui.node.OwnerSnapshotObserver.withNoSnapshotReadObservation$ui_debug(OwnerSnapshotObserver.kt:49)
at androidx.compose.ui.node.LayoutNode.withNoSnapshotReadObservation$ui_debug(LayoutNode.kt:1054)
at androidx.compose.ui.layout.SubcomposeLayoutState.subcompose(SubcomposeLayout.kt:158)
at androidx.compose.ui.layout.SubcomposeLayoutState.subcompose(SubcomposeLayout.kt:152)
at androidx.compose.foundation.layout.BoxWithConstraintsKt$BoxWithConstraints$1$1.invoke-0kLqBqw(BoxWithConstraints.kt:61)
at androidx.compose.foundation.layout.BoxWithConstraintsKt$BoxWithConstraints$1$1.invoke(BoxWithConstraints.kt:59)
at androidx.compose.ui.layout.SubcomposeLayoutState$createMeasurePolicy$1.measure-3p2s80s(SubcomposeLayout.kt:212)
at androidx.compose.ui.node.InnerPlaceable.measure-BRTryo0(InnerPlaceable.kt:45)
at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:95)
at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:94)
at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:121)
at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_debug(OwnerSnapshotObserver.kt:75)
at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_debug(OwnerSnapshotObserver.kt:63)
at androidx.compose.ui.node.OuterMeasurablePlaceable.remeasure-BRTryo0(OuterMeasurablePlaceable.kt:94)
at androidx.compose.ui.node.OuterMeasurablePlaceable.measure-BRTryo0(OuterMeasurablePlaceable.kt:68)
at androidx.compose.ui.node.LayoutNode.measure-BRTryo0(LayoutNode.kt:1172)
at androidx.compose.foundation.layout.BoxKt$boxMeasurePolicy$1.measure-3p2s80s(Box.kt:111)
at androidx.compose.ui.node.InnerPlaceable.measure-BRTryo0(InnerPlaceable.kt:45)
at androidx.compose.ui.graphics.SimpleGraphicsLayerModifier.measure-3p2s80s(GraphicsLayerModifier.kt:213)
at androidx.compose.ui.node.ModifiedLayoutNode.measure-BRTryo0(ModifiedLayoutNode.kt:37)
at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:108)
E/AndroidRuntime: at androidx.compose.ui.graphics.SimpleGraphicsLayerModifier.measure-3p2s80s(GraphicsLayerModifier.kt:213)
at androidx.compose.ui.node.ModifiedLayoutNode.measure-BRTryo0(ModifiedLayoutNode.kt:37)
at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:95)
at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:94)
at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:121)
at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_debug(OwnerSnapshotObserver.kt:75)
at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_debug(OwnerSnapshotObserver.kt:63)
at androidx.compose.ui.node.OuterMeasurablePlaceable.remeasure-BRTryo0(OuterMeasurablePlaceable.kt:94)
at androidx.compose.ui.node.OuterMeasurablePlaceable.measure-BRTryo0(OuterMeasurablePlaceable.kt:68)
at androidx.compose.ui.node.LayoutNode.measure-BRTryo0(LayoutNode.kt:1172)
at androidx.compose.ui.layout.RootMeasurePolicy.measure-3p2s80s(RootMeasurePolicy.kt:38)
at androidx.compose.ui.node.InnerPlaceable.measure-BRTryo0(InnerPlaceable.kt:45)
at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:108)
at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:108)
at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:108)
at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:108)
at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:95)
at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:94)
at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:1787)
at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:116)
at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_debug(OwnerSnapshotObserver.kt:75)
at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_debug(OwnerSnapshotObserver.kt:63)
at androidx.compose.ui.node.OuterMeasurablePlaceable.remeasure-BRTryo0(OuterMeasurablePlaceable.kt:94)
at androidx.compose.ui.node.LayoutNode.remeasure-BRTryo0$ui_debug(LayoutNode.kt:1179)
at androidx.compose.ui.node.MeasureAndLayoutDelegate.doRemeasure-0kLqBqw(MeasureAndLayoutDelegate.kt:170)
at androidx.compose.ui.node.MeasureAndLayoutDelegate.access$doRemeasure-0kLqBqw(MeasureAndLayoutDelegate.kt:39)
at androidx.compose.ui.node.MeasureAndLayoutDelegate.measureAndLayout(MeasureAndLayoutDelegate.kt:212)
at androidx.compose.ui.platform.AndroidComposeView.onMeasure(AndroidComposeView.android.kt:543)
at android.view.View.measure(View.java:25466)
at androidx.compose.ui.platform.AbstractComposeView.onMeasure(ComposeView.android.kt:249)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:747)
at android.view.View.measure(View.java:25466)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3397)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2228)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2486)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 17270 SIG:

I ran into a similar issue where a #Composable annotated expected function was not visible at runtime in a different Gradle module. Hence the java.lang.NoSuchMethodError exception. The solution was to make the expected function internal and have a public wrapper function delegate to that internal function.
Crashes:
#Composable
expect fun Image() { ... }
Does not crash:
#Composable
internal expect fun InternalImage() { ... }
#Composable
fun Image() {
InternalImage()
}

Related

App keeps stopping in the emulator and compiler doesn't throw an error. Logcat included

Here is the log. Can you please help me and tell me why this occur ? And how i can solve this.
And the compose preview doesn't work too.
I hope you can help me.
2023-01-22 17:25:58.191 9255-9255 AndroidRuntime E FATAL EXCEPTION: main
Process: com.example., PID: 9255
java.lang.IllegalArgumentException: The initial value must have an associated anchor.
at androidx.wear.compose.material.SwipeableState.ensureInit$compose_material_release(Swipeable.kt:146)
at androidx.wear.compose.material.SwipeableKt$swipeable$3.invoke(Swipeable.kt:544)
at androidx.wear.compose.material.SwipeableKt$swipeable$3.invoke(Swipeable.kt:523)
at androidx.compose.ui.ComposedModifierKt$materialize$result$1.invoke(ComposedModifier.kt:263)
at androidx.compose.ui.ComposedModifierKt$materialize$result$1.invoke(ComposedModifier.kt:258)
at androidx.compose.ui.Modifier$Element$DefaultImpls.foldIn(Modifier.kt:107)
at androidx.compose.ui.ComposedModifier.foldIn(ComposedModifier.kt:172)
at androidx.compose.ui.CombinedModifier.foldIn(Modifier.kt:149)
at androidx.compose.ui.ComposedModifierKt.materialize(ComposedModifier.kt:258)
at androidx.compose.ui.layout.LayoutKt$materializerOf$1.invoke-Deg8D_g(Layout.kt:226)
at androidx.compose.ui.layout.LayoutKt$materializerOf$1.invoke(Layout.kt:225)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at com.example..presentation.MainActivityKt.OnTouch(MainActivity.kt:124)
at com.example..presentation.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:37)
at com.example..presentation.ComposableSingletons$MainActivityKt$lambda-1$1.invoke(MainActivity.kt:34)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:410)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:252)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:251)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:174)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:123)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:122)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:114)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:157)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:156)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:156)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:140)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
2023-01-22 17:25:58.191 9255-9255 AndroidRuntime com.example. E at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:74)
at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3061)
at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3051)
at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:252)
at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1)
at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3051)
at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:2988)
at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:568)
at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:811)
at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:503)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:140)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:131)
at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:961)
at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:131)
at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:182)
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.android.kt:138)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:131)
at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:1048)
at android.view.View.dispatchAttachedToWindow(View.java:20510)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3493)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3500)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3500)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3500)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2433)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1968)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8199)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2023-01-22 17:25:59.595 9255-9271 System com.example W A resource failed to call close.
2023-01-22 17:26:02.850 9255-9276 . com.example I Waiting for a blocking GC ProfileSaver
I think this part of my code cause the trouble:
#OptIn(ExperimentalWearMaterialApi::class)
#Composable
fun OnTouch() {
val horizontalSwipeState =
SwipeableState("initial")
val horizontalAnchors = mapOf(0f to "left",
1f to "right")
val verticalSwipeState = SwipeableState("initial")
val verticalAnchors = mapOf(0f to "up", 1f to "down")
Box(
modifier = Modifier
.swipeable(
state = horizontalSwipeState,
anchors = horizontalAnchors,
orientation = Orientation.Horizontal,
enabled = true,
reverseDirection = false,
interactionSource = null,
thresholds = { _, _ -> FractionalThreshold(0.5f) },
resistance = resistanceConfig(horizontalAnchors.keys),
velocityThreshold = Dp(200f)
)
.then(
Modifier.swipeable(
state = verticalSwipeState,
anchors = verticalAnchors,
orientation = Orientation.Vertical,
enabled = true,
reverseDirection = false,
interactionSource = null,
thresholds = { _, _ -> FractionalThreshold(0.5f) },
resistance = resistanceConfig(verticalAnchors.keys),
velocityThreshold = Dp(200f)
)
)
) {
println(verticalSwipeState.currentValue)
println(verticalSwipeState.currentValue)
}
}
If you need more Information just tell me i try then to provide them for you.
Thanks for your help !
Michael

The onItemChangeListener crashes

So I am currently working on a spinner that changes entries in a list. When loading into the Fragment and loading the List into the spinner adapter and setting up the onItemChangedListener everything works fine. But when the App goes to another Fragment and then goes back the app crashes with the following exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.heffnersoftware.hsproduction, PID: 13111
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter view
at de.heffnersoftware.hsproduction.ui.storeManagement.ListFiveFragment$onCreateView$3.onItemSelected(Unknown Source:2)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:957)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:946)
at android.widget.AdapterView.selectionChanged(AdapterView.java:935)
at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1118)
at android.widget.AdapterView.handleDataChanged(AdapterView.java:1094)
at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
at android.widget.Spinner.onMeasure(Spinner.java:614)
at androidx.appcompat.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:438)
at android.view.View.measure(View.java:24721)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
at android.view.View.measure(View.java:24721)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:811)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:466)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.measureChildren(BasicMeasure.java:134)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:278)
at androidx.constraintlayout.core.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:120)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1594)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1708)
at android.view.View.measure(View.java:24721)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:24721)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:811)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:466)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.measureChildren(BasicMeasure.java:134)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:278)
at androidx.constraintlayout.core.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:120)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1594)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1708)
at android.view.View.measure(View.java:24721)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:760)
at com.google.android.material.appbar.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:99)
at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:2227)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:831)
at android.view.View.measure(View.java:24721)
at androidx.drawerlayout.widget.DrawerLayout.onMeasure(DrawerLayout.java:1156)
at android.view.View.measure(View.java:24721)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
at android.view.View.measure(View.java:24721)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:24721)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:24721)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:24721)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6903)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:743)
at android.view.View.measure(View.java:24721)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3135)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1900)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2200)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1788)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7867)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:967)
at android.view.Choreographer.doCallbacks(Choreographer.java:791)
at android.view.Choreographer.doFrame(Choreographer.java:726)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7399)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
Bellow is the Listener and code surrounding it:
val spinner: Spinner = root.findViewById(R.id.store_select_spinner)
val spinnerAdapter: ArrayAdapter<String> = ArrayAdapter(
requireContext(), R.layout.spinner_item, llNames
)
spinnerAdapter.setDropDownViewResource(R.layout.spinner_item)
spinner.adapter = spinnerAdapter
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View,
position: Int,
id: Long
) {
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
The error has been solved by the solution Demigod provided.
The code needs to be altered as the following:
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
StoreListFragment.storeAdapter.changeShelve(llShelveNames[position].first)
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}

Jetpack Compose how to remove focused TextField inside LazyColumn

Let's say I have a LazyColumn with Text field and button to toggle is the text field available or not.
LazyColumn() {
item {
var text by remember { mutableStateOf("Hello") }
if (myViewModel.showTextField.value) {
TextField(value = text, onValueChange = { text = it })
}
TextButton(onClick = { myViewModel.toggleField() }) {
Text(text = "toggle")
}
}
}
And viewModel has code like this
class MyViewModel : ViewModel() {
private val _showTextField = mutableStateOf(true)
val showTextField : State<Boolean> = _showTextField
fun toggleField() {
_showTextField.value = !showTextField.value
}
}
And now when I click on TextField before clicking toggle button. So TextField has focus before removing it, the application crashes with following error:
2022-04-09 19:43:03.417 12336-12336/com.courtesy.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.courtesy.test, PID: 12336
java.lang.IllegalStateException: LayoutCoordinates androidx.compose.ui.node.ModifierLocalConsumerNode#bab1d96 is not attached!
at androidx.compose.ui.node.LayoutNodeWrapper.localBoundingBoxOf(LayoutNodeWrapper.kt:714)
at androidx.compose.foundation.gestures.ContentInViewModifier.onSizeChanged-O0kMr_c(Scrollable.kt:524)
at androidx.compose.foundation.gestures.ContentInViewModifier.onRemeasured-ozmzZPI(Scrollable.kt:492)
at androidx.compose.ui.node.LayoutNodeWrapper$onMeasured$invokeRemeasureCallbacks$1.invoke(LayoutNodeWrapper.kt:244)
at androidx.compose.ui.node.LayoutNodeWrapper$onMeasured$invokeRemeasureCallbacks$1.invoke(LayoutNodeWrapper.kt:242)
at androidx.compose.runtime.snapshots.SnapshotStateObserver.withNoObservations(SnapshotStateObserver.kt:142)
at androidx.compose.ui.node.OwnerSnapshotObserver.withNoSnapshotReadObservation$ui_release(OwnerSnapshotObserver.kt:55)
at androidx.compose.ui.node.LayoutNodeWrapper.onMeasured(LayoutNodeWrapper.kt:247)
at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:107)
at androidx.compose.ui.graphics.SimpleGraphicsLayerModifier.measure-3p2s80s(GraphicsLayerModifier.kt:405)
at androidx.compose.ui.node.ModifiedLayoutNode.measure-BRTryo0(ModifiedLayoutNode.kt:40)
at androidx.compose.ui.node.LayoutNode$performMeasure$1.invoke(LayoutNode.kt:1342)
at androidx.compose.ui.node.LayoutNode$performMeasure$1.invoke(LayoutNode.kt:1341)
at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:1918)
at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:121)
at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_release(OwnerSnapshotObserver.kt:76)
at androidx.compose.ui.node.LayoutNode.performMeasure-BRTryo0$ui_release(LayoutNode.kt:1341)
at androidx.compose.ui.node.OuterMeasurablePlaceable.remeasure-BRTryo0(OuterMeasurablePlaceable.kt:94)
at androidx.compose.ui.node.LayoutNode.remeasure-_Sx5XlM$ui_release(LayoutNode.kt:1295)
at androidx.compose.ui.node.LayoutNode.remeasure-_Sx5XlM$ui_release$default(LayoutNode.kt:1291)
at androidx.compose.ui.node.MeasureAndLayoutDelegate.doRemeasure-sdFAvZA(MeasureAndLayoutDelegate.kt:180)
at androidx.compose.ui.node.MeasureAndLayoutDelegate.remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:256)
at androidx.compose.ui.node.MeasureAndLayoutDelegate.access$remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:36)
at androidx.compose.ui.node.MeasureAndLayoutDelegate.measureAndLayout(MeasureAndLayoutDelegate.kt:201)
at androidx.compose.ui.platform.AndroidComposeView.onMeasure(AndroidComposeView.android.kt:756)
at android.view.View.measure(View.java:25466)
at androidx.compose.ui.platform.AbstractComposeView.internalOnMeasure$ui_release(ComposeView.android.kt:298)
at androidx.compose.ui.platform.AbstractComposeView.onMeasure(ComposeView.android.kt:285)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
2022-04-09 19:43:03.418 12336-12336/com.courtesy.test E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25466)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:747)
at android.view.View.measure(View.java:25466)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3397)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2228)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2486)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
at android.view.Choreographer.doCallbacks(Choreographer.java:796)
at android.view.Choreographer.doFrame(Choreographer.java:731)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2022-04-09 19:43:03.816 12336-12336/com.courtesy.test I/Process: Sending signal. PID: 12336 SIG: 9
It works fine when I don't click the TextField before clicking the button.
And also everything works correctly if I use Column instead of LazyColumn.
Any ideas how to solve this issue?
Fixed by changing compose version from 1.2.0-alpha06 to 1.2.0-alpha07

Jetpack compose preview crashes with hiltViewModel<>()

I use compose version 1.1.0-beta03 and hilt-navigation-compose:1.0.0-beta01.
this is my Composable code:
fun EngagementBotChart(modifier: Modifier = Modifier) {
val newHomeViewModel = hiltViewModel<HomeViewModel>()
AndroidView(
modifier = modifier.fillMaxSize(),
factory = { context ->
},
update = { view ->
}
)
}
it works fine when I build the project but this is what occurs in compose preview:
java.lang.NoSuchMethodException: ir.inbo.app.home.viewmodel.HomeViewModel.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3349)
at java.base/java.lang.Class.newInstance(Class.java:556)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at androidx.lifecycle.viewmodel.compose.ViewModelKt.get(ViewModel.kt:87)
at androidx.lifecycle.viewmodel.compose.ViewModelKt.viewModel(ViewModel.kt:72)
at ir.inbo.app.home.ui.HomeComponentsKt.EngagementBotChart(HomeComponents.kt:281)
at ir.inbo.app.home.ui.ComposableSingletons$HomeComponentsKt$lambda-1$1$1$4$invoke$$inlined$ConstraintLayout$2.invoke(ConstraintLayout.kt:2424)
at ir.inbo.app.home.ui.ComposableSingletons$HomeComponentsKt$lambda-1$1$1$4$invoke$$inlined$ConstraintLayout$2.invoke(ConstraintLayout.kt:101)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.ui.layout.LayoutKt.MultiMeasureLayout(Layout.kt:562)
at ir.inbo.app.home.ui.ComposableSingletons$HomeComponentsKt$lambda-1$1$1$4.invoke(HomeComponents.kt:255)
at ir.inbo.app.home.ui.ComposableSingletons$HomeComponentsKt$lambda-1$1$1$4.invoke(HomeComponents.kt:86)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at ir.inbo.app.home.ui.HomeComponentsKt.HomeBoxComponent(HomeComponents.kt:267)
at ir.inbo.app.home.ui.ComposableSingletons$HomeComponentsKt$lambda-1$1$invoke$$inlined$ConstraintLayout$2.invoke(ConstraintLayout.kt:2413)
at ir.inbo.app.home.ui.ComposableSingletons$HomeComponentsKt$lambda-1$1$invoke$$inlined$ConstraintLayout$2.invoke(ConstraintLayout.kt:101)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.ui.layout.LayoutKt.MultiMeasureLayout(Layout.kt:562)
at ir.inbo.app.home.ui.ComposableSingletons$HomeComponentsKt$lambda-1$1.invoke(HomeComponents.kt:255)
at ir.inbo.app.home.ui.ComposableSingletons$HomeComponentsKt$lambda-1$1.invoke(HomeComponents.kt:34)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.material.MaterialTheme_androidKt.PlatformMaterialTheme(MaterialTheme.android.kt:23)
at androidx.compose.material.MaterialThemeKt$MaterialTheme$1$1.invoke(MaterialTheme.kt:82)
at androidx.compose.material.MaterialThemeKt$MaterialTheme$1$1.invoke(MaterialTheme.kt:81)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.material.TextKt.ProvideTextStyle(Text.kt:265)
at androidx.compose.material.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:81)
at androidx.compose.material.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:80)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.material.MaterialThemeKt.MaterialTheme(MaterialTheme.kt:72)
at ir.inbo.app.theme.ThemeKt.InboTheme(Theme.kt:54)
at ir.inbo.app.home.ui.HomeComponentsKt.HomeGrid(HomeComponents.kt:34)
at ir.inbo.app.home.ui.HomeComponentsKt.HomeGridPreview(HomeComponents.kt:232)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at androidx.compose.ui.tooling.CommonPreviewUtils.invokeComposableMethod(CommonPreviewUtils.kt:150)
at androidx.compose.ui.tooling.CommonPreviewUtils.invokeComposableViaReflection$ui_tooling_release(CommonPreviewUtils.kt:189)
at androidx.compose.ui.tooling.ComposeViewAdapter$init$3$1$composable$1.invoke(ComposeViewAdapter.kt:578)
at androidx.compose.ui.tooling.ComposeViewAdapter$init$3$1$composable$1.invoke(ComposeViewAdapter.kt:576)
at androidx.compose.ui.tooling.ComposeViewAdapter$init$3$1.invoke(ComposeViewAdapter.kt:615)
at androidx.compose.ui.tooling.ComposeViewAdapter$init$3$1.invoke(ComposeViewAdapter.kt:571)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.tooling.InspectableKt.Inspectable(Inspectable.kt:61)
at androidx.compose.ui.tooling.ComposeViewAdapter$WrapPreview$1.invoke(ComposeViewAdapter.kt:520)
at androidx.compose.ui.tooling.ComposeViewAdapter$WrapPreview$1.invoke(ComposeViewAdapter.kt:519)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.tooling.ComposeViewAdapter.WrapPreview(ComposeViewAdapter.kt:515)
at androidx.compose.ui.tooling.ComposeViewAdapter.access$WrapPreview(ComposeViewAdapter.kt:121)
at androidx.compose.ui.tooling.ComposeViewAdapter$init$3.invoke(ComposeViewAdapter.kt:571)
at androidx.compose.ui.tooling.ComposeViewAdapter$init$3.invoke(ComposeViewAdapter.kt:568)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:410)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:252)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:251)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:166)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:123)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:122)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:114)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:157)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:156)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:156)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:140)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.ComposerKt.invokeComposable(Composer.kt:3337)
at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2582)
at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2571)
at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:234)
at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source)
at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2571)
at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:2522)
at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:478)
at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:748)
at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:433)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:140)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:131)
at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:897)
at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:131)
at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:182)
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.android.kt:138)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:131)
at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:984)
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.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:44)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:360)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:714)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:870)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:187)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
when I delete the hiltviewModel code everything work fine!
I try it with different versions of compose and I can't solve this problem!
After some searching and reviewing of google sample codes here is the way you must follow to prevent the preview crashes:
Have a root composable for your screen and initial view model there, collect your data from viewModel as state (in case your data is flow) then pass data to content composable
#Composable
fun HomeScreen(
navController: NavController
){
val viewModel = hiltViewModel<HomeViewModel>()
val someData by viewModel.someData.collectAsState()
}
#Composable
fun HomeScreenContent(
someData: Type = defaultvalue
){
//your UI codes
}
in preview use, HomeScreenContent instead of HomeScreen, and your problem will be solved!
thanks to #PhilipDukhov that helped in the comments

androidx.navigation.NavGraph cannot be cast to androidx.navigation.compose.ComposeNavigator$Destination

After updating navigation compose dependency from 2.4.0-alpha03 to 2.4.0-alpha05 I've encountered this strange error after trying to navigate between composable screens (For example navigate from taskComposable to listComposable screen). Here's a part of my code.
MainActivity:
#AndroidEntryPoint
class MainActivity : ComponentActivity() {
private lateinit var navController: NavHostController
#ExperimentalAnimationApi
#ExperimentalMaterialApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyComposeDemoTheme {
navController = rememberNavController()
SetupNavigation(
navController = navController
)
}
}
}
}
SetupNavigation:
#ExperimentalAnimationApi
#ExperimentalMaterialApi
#Composable
fun SetupNavigation(
navController: NavHostController
) {
val screen = remember(navController) { Screens(navController) }
NavHost(
navController = navController,
startDestination = "list/{action}"
) {
listComposable(
navigateToTaskScreen = screen.task
)
taskComposable(
navigateToListScreen = screen.list
)
}
}
Screens:
class Screens(navController: NavHostController) {
val list: (Action) -> Unit = { action ->
navController.navigate("list/${action.name}"){
popUpTo(LIST_SCREEN) { inclusive = true }
}
}
val task: (Int) -> Unit = { taskId ->
navController.navigate("task/$taskId")
}
}
taskComposable
fun NavGraphBuilder.taskComposable(
navigateToListScreen: (Action) -> Unit
) {
composable(
route = TASK_SCREEN,
arguments = listOf(navArgument(TASK_ID_ARGUMENT_KEY) {
type = NavType.IntType
})
) {
TaskScreen(
navigateToListScreen = navigateToListScreen
)
}
}
Error:
Process: com.example.mycomposedemo, PID: 2413
java.lang.ClassCastException: androidx.navigation.NavGraph cannot be cast to androidx.navigation.compose.ComposeNavigator$Destination
at androidx.navigation.compose.NavHostKt$NavHost$6$1.invoke(NavHost.kt:146)
at androidx.navigation.compose.NavHostKt$NavHost$6$1.invoke(NavHost.kt:145)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
at androidx.compose.runtime.saveable.SaveableStateHolderImpl.SaveableStateProvider(SaveableStateHolder.kt:84)
at androidx.navigation.compose.NavBackStackEntryProviderKt.SaveableStateProvider(NavBackStackEntryProvider.kt:59)
at androidx.navigation.compose.NavBackStackEntryProviderKt.access$SaveableStateProvider(NavBackStackEntryProvider.kt:1)
at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke(NavBackStackEntryProvider.kt:51)
at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke(NavBackStackEntryProvider.kt:50)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
at androidx.navigation.compose.NavBackStackEntryProviderKt.LocalOwnersProvider(NavBackStackEntryProvider.kt:46)
at androidx.navigation.compose.NavHostKt$NavHost$6.invoke(NavHost.kt:145)
at androidx.navigation.compose.NavHostKt$NavHost$6.invoke(NavHost.kt:144)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.animation.CrossfadeKt$Crossfade$1$1.invoke(Crossfade.kt:74)
at androidx.compose.animation.CrossfadeKt$Crossfade$1$1.invoke(Crossfade.kt:69)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.animation.CrossfadeKt.Crossfade(Crossfade.kt:86)
at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:144)
at androidx.navigation.compose.NavHostKt$NavHost$7.invoke(Unknown
Source:13)
at androidx.navigation.compose.NavHostKt$NavHost$7.invoke(Unknown
Source:10)
at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:140)
at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2156)
at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2399)
at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2580)
at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2573)
at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(SnapshotState.kt:540)
at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2566)
at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:2542)
at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:613)
at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:764)
at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:103)
at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:447)
at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:416)
at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:34)
at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
2021-07-29 10:00:14.416 2413-2413/com.example.mycomposedemo
E/AndroidRuntime: at
androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:964)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:721)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
This is an internal bug which will be fixed in Navigation 2.4.0-alpha06 release. #headsvk shared the issue link: https://issuetracker.google.com/issues/194301889

Categories

Resources