https://kotlinlang.org logo
#compose
Title
# compose
t

theapache64

03/30/2022, 5:57 PM
Getting below error when accessing
layoutCoordinates.positionInRoot()
. This is only happening in release build.
Copy code
Fatal Exception: java.lang.IllegalStateException
LayoutCoordinate operations are only valid when isAttached is true
compose version =
1.1.0-rc01
I can wrap it inside a
if(layoutCoordinates.isAttached)
but am thinking why this is happening only on release build? 🤔
d

Doris Liu

03/31/2022, 7:14 PM
Do you have a snippet to repro this?
t

theapache64

03/31/2022, 7:15 PM
I haven’t tried this in isolation 🤔 Will try and post the result here 👍
but any idea why it happened only in release build? 🤔
I have seen similar issue closed in one of the prev releases -> https://issuetracker.google.com/issues/182551468
d

Doris Liu

03/31/2022, 7:20 PM
Only in release build? That seems odd. Do you have a stack trace?
t

theapache64

03/31/2022, 7:22 PM
Copy code
androidx.compose.ui.node.LayoutNodeWrapper.localToRoot-MK-Hz9U (LayoutNodeWrapper.kt:767)
androidx.compose.ui.layout.LayoutCoordinatesKt.positionInRoot (LayoutCoordinatesKt.java:103)
com.my.company.TrackModifierKt$track$1$2$1.invokeSuspend (TrackModifier.kt:104) 
kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:33)
kotlinx.coroutines.DispatchedTask.run (DispatchedTask.kt:106)
androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch (AndroidUiDispatcher.android.kt:81)
androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch (AndroidUiDispatcher.android.kt:41)
androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run (AndroidUiDispatcher.android.kt:57)
android.os.Handler.handleCallback (Handler.java:938)
android.os.Handler.dispatchMessage (Handler.java:99)
android.os.Looper.loop (Looper.java:246)
android.app.ActivityThread.main (ActivityThread.java:8633)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
So
TrackModifier.kt:104
has a
positionInRoot()
call inside a
onGloballyPositioned
modifier
d

Doris Liu

03/31/2022, 7:29 PM
Are you doing the query in the
onGloballyPositioned
callback, or did you cache the layoutCoordinates and used it at a different time?
t

theapache64

03/31/2022, 7:32 PM
Ahh.. now am getting it. There’s coroutine involved here. The layoutCoordinates are used just after the launch. During the scroll the slight delay might be causing the issue. but am wondering why it wasn’t happening before 🤔
Anyways, that should be the reason.
d

Doris Liu

03/31/2022, 7:33 PM
Yea, the
positionInRoot
only guarantees correctness if you query it after the layout pass is done.
t

theapache64

03/31/2022, 7:34 PM
what if i use the
isAttached
flag before accessing
positionInRoot
? would that help me with the crash. ?
d

Doris Liu

03/31/2022, 7:35 PM
That'll probably work. By the time the node you are querying is attached, all its ancestors would've been attached also.
t

theapache64

03/31/2022, 7:35 PM
Cool 👍 Will give it a try.
257 Views