英文:
Getting an IllegalStateException exception with BasicTextField in Jetpack Compose
问题
我有一个带有提示的 BasicTextField。然而,出于某种原因,当我输入一些文本,然后删除一切并尝试重新输入时,应用程序崩溃了。不知道如何修复它,任何帮助都将不胜感激
我使用的是 2023.04.01 的 compose BOM,因此我相信问题不在版本中。
我的 Composable:
@Composable
fun EditableText(
text: String,
hint: String,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier
) {
BasicTextField(
modifier = modifier,
value = text,
onValueChange = onValueChange,
textStyle = headline.copy(color = Color.White, textAlign = TextAlign.Start),
singleLine = true,
decorationBox = { innerTextField ->
if (text.isEmpty()) {
Text(
textAlign = TextAlign.Start,
text = hint,
style = headline.copy(
color = Color.White.copy(alpha = 0.75f),
textAlign = TextAlign.Start
)
)
} else {
innerTextField()
}
}
)
}
错误信息:
FATAL EXCEPTION: main
Process: com.pm.savings, PID: 7045
java.lang.IllegalStateException: LayoutCoordinate operations are only valid when isAttached is true
at androidx.compose.ui.node.NodeCoordinator.localToRoot-MK-Hz9U(NodeCoordinator.kt:881)
at androidx.compose.foundation.text.TextFieldDelegate$Companion.notifyFocusedRect$foundation_release(TextFieldDelegate.kt:173)
at androidx.compose.foundation.text.CoreTextFieldKt.notifyFocusedRect(CoreTextField.kt:1077)
at androidx.compose.foundation.text.CoreTextFieldKt.access$notifyFocusedRect(CoreTextField.kt:1)
at androidx.compose.foundation.text.CoreTextFieldKt$CoreTextField$5$1$1$2.measure-3p2s80s(CoreTextField.kt:610)
at androidx.compose.ui.node.InnerNodeCoordinator.measure-BRTryo0(InnerNodeCoordinator.kt:103)
at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1090)
at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1086)
at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2200)
at androidx.compose.runtime.snapshots.SnapshotStateObserver$observeReads$1$1.invoke(SnapshotStateObserver.kt:234)
at androidx.compose.runtime.snapshots.SnapshotStateObserver$observeReads$1$1.invoke(SnapshotStateObserver.kt:230)
at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:341)
at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1)
at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:230)
at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:120)
英文:
I have a BasicTextField with a hint. However, for some reason when I put some text in, then delete everything and try to input again, the app crashes. Have no idea how to fix it, any help would be much appreciated
I use compose BOM of 2023.04.01, so I believe the issue is not in the version.
My Composable:
@Composable
fun EditableText(
text: String,
hint: String,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier
) {
BasicTextField(
modifier = modifier,
value = text,
onValueChange = onValueChange,
textStyle = headline.copy(color = Color.White, textAlign = TextAlign.Start),
singleLine = true,
decorationBox = { innerTextField ->
if (text.isEmpty()) {
Text(
textAlign = TextAlign.Start,
text = hint,
style = headline.copy(
color = Color.White.copy(alpha = 0.75f),
textAlign = TextAlign.Start
)
)
} else {
innerTextField()
}
}
)
}
The error:
FATAL EXCEPTION: main
Process: com.pm.savings, PID: 7045
java.lang.IllegalStateException: LayoutCoordinate operations are only valid when isAttached is true
at androidx.compose.ui.node.NodeCoordinator.localToRoot-MK-Hz9U(NodeCoordinator.kt:881)
at androidx.compose.foundation.text.TextFieldDelegate$Companion.notifyFocusedRect$foundation_release(TextFieldDelegate.kt:173)
at androidx.compose.foundation.text.CoreTextFieldKt.notifyFocusedRect(CoreTextField.kt:1077)
at androidx.compose.foundation.text.CoreTextFieldKt.access$notifyFocusedRect(CoreTextField.kt:1)
at androidx.compose.foundation.text.CoreTextFieldKt$CoreTextField$5$1$1$2.measure-3p2s80s(CoreTextField.kt:610)
at androidx.compose.ui.node.InnerNodeCoordinator.measure-BRTryo0(InnerNodeCoordinator.kt:103)
at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1090)
at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1086)
at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2200)
at androidx.compose.runtime.snapshots.SnapshotStateObserver$observeReads$1$1.invoke(SnapshotStateObserver.kt:234)
at androidx.compose.runtime.snapshots.SnapshotStateObserver$observeReads$1$1.invoke(SnapshotStateObserver.kt:230)
at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:341)
at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1)
at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:230)
at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:120)
答案1
得分: 0
你应该在text
为空时返回innerTextField
,因为它用于设置焦点,只需移除else
条件,它就能同时适用于两种情况。
decorationBox = { innerTextField ->
if (text.isEmpty()) {
Text(
textAlign = TextAlign.Start,
text = hint,
style = headline.copy(
color = Color.White.copy(alpha = 0.75f),
textAlign = TextAlign.Start
)
)
}
innerTextField()
}
英文:
You should return innerTextField
even when the text
is empty which uses it to set focus, just remove else condition and it should work for both cases
decorationBox = { innerTextField ->
if (text.isEmpty()) {
Text(
textAlign = TextAlign.Start,
text = hint,
style = headline.copy(
color = Color.White.copy(alpha = 0.75f),
textAlign = TextAlign.Start
)
)
}
innerTextField()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论