在Jetpack Compose中使用BasicTextField时出现了IllegalStateException异常。

huangapple go评论96阅读模式
英文:

Getting an IllegalStateException exception with BasicTextField in Jetpack Compose

问题

我有一个带有提示的 BasicTextField。然而,出于某种原因,当我输入一些文本,然后删除一切并尝试重新输入时,应用程序崩溃了。不知道如何修复它,任何帮助都将不胜感激 在Jetpack Compose中使用BasicTextField时出现了IllegalStateException异常。

我使用的是 2023.04.01 的 compose BOM,因此我相信问题不在版本中。

我的 Composable:

  1. @Composable
  2. fun EditableText(
  3. text: String,
  4. hint: String,
  5. onValueChange: (String) -> Unit,
  6. modifier: Modifier = Modifier
  7. ) {
  8. BasicTextField(
  9. modifier = modifier,
  10. value = text,
  11. onValueChange = onValueChange,
  12. textStyle = headline.copy(color = Color.White, textAlign = TextAlign.Start),
  13. singleLine = true,
  14. decorationBox = { innerTextField ->
  15. if (text.isEmpty()) {
  16. Text(
  17. textAlign = TextAlign.Start,
  18. text = hint,
  19. style = headline.copy(
  20. color = Color.White.copy(alpha = 0.75f),
  21. textAlign = TextAlign.Start
  22. )
  23. )
  24. } else {
  25. innerTextField()
  26. }
  27. }
  28. )
  29. }

错误信息:

  1. FATAL EXCEPTION: main
  2. Process: com.pm.savings, PID: 7045
  3. java.lang.IllegalStateException: LayoutCoordinate operations are only valid when isAttached is true
  4. at androidx.compose.ui.node.NodeCoordinator.localToRoot-MK-Hz9U(NodeCoordinator.kt:881)
  5. at androidx.compose.foundation.text.TextFieldDelegate$Companion.notifyFocusedRect$foundation_release(TextFieldDelegate.kt:173)
  6. at androidx.compose.foundation.text.CoreTextFieldKt.notifyFocusedRect(CoreTextField.kt:1077)
  7. at androidx.compose.foundation.text.CoreTextFieldKt.access$notifyFocusedRect(CoreTextField.kt:1)
  8. at androidx.compose.foundation.text.CoreTextFieldKt$CoreTextField$5$1$1$2.measure-3p2s80s(CoreTextField.kt:610)
  9. at androidx.compose.ui.node.InnerNodeCoordinator.measure-BRTryo0(InnerNodeCoordinator.kt:103)
  10. at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1090)
  11. at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1086)
  12. at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2200)
  13. at androidx.compose.runtime.snapshots.SnapshotStateObserver$observeReads$1$1.invoke(SnapshotStateObserver.kt:234)
  14. at androidx.compose.runtime.snapshots.SnapshotStateObserver$observeReads$1$1.invoke(SnapshotStateObserver.kt:230)
  15. at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:341)
  16. at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1)
  17. at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:230)
  18. 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 在Jetpack Compose中使用BasicTextField时出现了IllegalStateException异常。

I use compose BOM of 2023.04.01, so I believe the issue is not in the version.

My Composable:

  1. @Composable
  2. fun EditableText(
  3. text: String,
  4. hint: String,
  5. onValueChange: (String) -> Unit,
  6. modifier: Modifier = Modifier
  7. ) {
  8. BasicTextField(
  9. modifier = modifier,
  10. value = text,
  11. onValueChange = onValueChange,
  12. textStyle = headline.copy(color = Color.White, textAlign = TextAlign.Start),
  13. singleLine = true,
  14. decorationBox = { innerTextField ->
  15. if (text.isEmpty()) {
  16. Text(
  17. textAlign = TextAlign.Start,
  18. text = hint,
  19. style = headline.copy(
  20. color = Color.White.copy(alpha = 0.75f),
  21. textAlign = TextAlign.Start
  22. )
  23. )
  24. } else {
  25. innerTextField()
  26. }
  27. }
  28. )
  29. }

The error:

  1. FATAL EXCEPTION: main
  2. Process: com.pm.savings, PID: 7045
  3. java.lang.IllegalStateException: LayoutCoordinate operations are only valid when isAttached is true
  4. at androidx.compose.ui.node.NodeCoordinator.localToRoot-MK-Hz9U(NodeCoordinator.kt:881)
  5. at androidx.compose.foundation.text.TextFieldDelegate$Companion.notifyFocusedRect$foundation_release(TextFieldDelegate.kt:173)
  6. at androidx.compose.foundation.text.CoreTextFieldKt.notifyFocusedRect(CoreTextField.kt:1077)
  7. at androidx.compose.foundation.text.CoreTextFieldKt.access$notifyFocusedRect(CoreTextField.kt:1)
  8. at androidx.compose.foundation.text.CoreTextFieldKt$CoreTextField$5$1$1$2.measure-3p2s80s(CoreTextField.kt:610)
  9. at androidx.compose.ui.node.InnerNodeCoordinator.measure-BRTryo0(InnerNodeCoordinator.kt:103)
  10. at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1090)
  11. at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1086)
  12. at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2200)
  13. at androidx.compose.runtime.snapshots.SnapshotStateObserver$observeReads$1$1.invoke(SnapshotStateObserver.kt:234)
  14. at androidx.compose.runtime.snapshots.SnapshotStateObserver$observeReads$1$1.invoke(SnapshotStateObserver.kt:230)
  15. at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:341)
  16. at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1)
  17. at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:230)
  18. at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:120)

答案1

得分: 0

你应该在text为空时返回innerTextField,因为它用于设置焦点,只需移除else条件,它就能同时适用于两种情况。

  1. decorationBox = { innerTextField ->
  2. if (text.isEmpty()) {
  3. Text(
  4. textAlign = TextAlign.Start,
  5. text = hint,
  6. style = headline.copy(
  7. color = Color.White.copy(alpha = 0.75f),
  8. textAlign = TextAlign.Start
  9. )
  10. )
  11. }
  12. innerTextField()
  13. }
英文:

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

  1. decorationBox = { innerTextField ->
  2. if (text.isEmpty()) {
  3. Text(
  4. textAlign = TextAlign.Start,
  5. text = hint,
  6. style = headline.copy(
  7. color = Color.White.copy(alpha = 0.75f),
  8. textAlign = TextAlign.Start
  9. )
  10. )
  11. }
  12. innerTextField()
  13. }

huangapple
  • 本文由 发表于 2023年5月28日 23:54:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定