"requestCursorUpdates is not supported"大纲文本字段错误



我有几个OutlinedTextField输入,在使用Data Class作为ViewModel中的状态持有人时,输入它们什么也不做。每次我从软键盘输入一个字符时,Logcat返回以下Debug错误。但是,Logcat不会抛出任何运行时异常。我该如何解决这个问题?

W/RecordingIC: requestCursorUpdates is not supported

数据类

data class ShoppingListItemState(
val name: String = "",
val category: String = "",
val quantity: String = "",
val unit: String = "",
val ppu: String = "",
val notes: String = "",
val hasImage: Boolean = false,
val imageUri: Uri? = null 
)

ViewModel

class ShoppingListScreenViewModel(): ViewModel() {
val shoppingListItemState = mutableStateOf(ShoppingListItemState())
fun setListItemStateValue(stateToEdit: String, stateValue: String) {
val item = shoppingListItemState.value
when (stateToEdit) {
"Name" -> item.copy(name = stateValue)
"Category" -> item.copy(category = stateValue)
"Quantity" -> item.copy(quantity = stateValue)
"Unit" -> item.copy(unit = stateValue)
"PPU" -> item.copy(ppu = stateValue)
"Notes" -> item.copy(notes = stateValue)
"HasImage" -> item.copy(hasImage =stateValue.toBoolean())
"ImageUri" -> item.copy(imageUri = stateValue.toUri())
}
shoppingListItemState.value = item 
}
}

CustomOutlinedTextField叫

val shoppingListScreenViewModel: ShoppingListScreenViewModel = viewModel()

CustomOutlinedTextField(
modifier = Modifier
.fillMaxWidth()
.onPreviewKeyEvent {
if (it.key == Key.Tab) {
focusManager.moveFocus(FocusDirection.Down)
true
} else {
false
}
},
label = stringResource(id = R.string.item_name),
inputVal = shoppingListScreenViewModel.shoppingListItemState.value.name,
isSingleLine = true,
keyboardOptions = KeyboardOptions.Default.copy(
capitalization = KeyboardCapitalization.Sentences,
autoCorrect = false,
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(
onNext = { focusManager.moveFocus(FocusDirection.Down) }
)
) { shoppingListScreenViewModel.setListItemStateValue("Name", it) }

CustomOutlinedTextField可组合

@Composable
fun CustomOutlinedTextField(
modifier: Modifier = Modifier,
label: String = "",
inputVal: String,
isSingleLine:
Boolean = false,
maxLines: Int = 0,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions.Default,
onValChange: (String) -> Unit
) {
OutlinedTextField(
value = inputVal,
onValueChange = { onValChange(it) },
label = { Text(text = label) },
singleLine = isSingleLine,
maxLines = maxLines,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
modifier = modifier
)
}

找到问题。我必须这样做:

fun setListItemStateValue(stateToEdit: String, stateValue: String) {
val item = shoppingListItemState.value
when (stateToEdit) {
"Name" -> shoppingListItemState.value = item.copy(name = stateValue)
"Category" -> shoppingListItemState.value = item.copy(category = stateValue)
"Quantity" -> shoppingListItemState.value = item.copy(quantity = stateValue)
"Unit" -> shoppingListItemState.value = item.copy(unit = stateValue)
"PPU" -> shoppingListItemState.value = item.copy(ppu = stateValue)
"Notes" -> shoppingListItemState.value = item.copy(notes = stateValue)
"HasImage" -> shoppingListItemState.value = item.copy(hasImage = stateValue.toBoolean())
"ImageUri" -> shoppingListItemState.value = item.copy(imageUri = stateValue.toUri())
}
}

我有同样的问题,当我添加一个ByteArray字段到我的房间表之一。希望这能帮助到一些人。

从这个开始:

@Entity  
data class FeatureXX(
@PrimaryKey(autoGenerate = true)
val id:Int,
val input0:String,
val inputImage: ByteArray
)
data class FeatureXXState(
val initialStateInput0:String = "",
val initialStateImage:ByteArray = ByteArray(0)
) 
sealed class ModifyFeatureXXEvent{
data class EnteredValueInput0(val value0:String):ModifyFeatureXXEvent()
data class EnteredImageValue(val imgValue:ByteArray):ModifyFeatureXXEvent()    
}
class ThisViewModel : VieModel(){
private val _state0 = mutableStateOf(FeatureXXState(initialStateInput0 = ""))
val state0: State<FeatureXXState> = _state0
private val _stateImage = mutableStateOf(initialStateImage = ByteArray(0))
val stateImage: State<FeatureXXState> = _stateImage
fun onEvent(event:ModifyFeatureXXEvent){
when(event){
is ModifyFeatureXXEvent.EnteredValueInput0 -> {
_state0.value = state0.value.copy(initialStateInput0 = event.value0)
}
is ModifyFeatureXXEvent.EnteredImageValue -> {
_stateImage.value = stateImage.value.copy(initialStateImage = event.imgValue)
}
}
}
}

我的Composables和你的很像。

如何解决我的问题->

@Entity  
data class FeatureXX(
@PrimaryKey(autoGenerate = true)
val id:Int,
val input0:String,
val inputImage: ByteArray
){
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FeatureXX
if (id != other.id) return false
if (input0 != other.input0) return false
if (!inputImage.contentEquals(other.inputImage)) return false
return true
}
override fun hashCode(): Int {
var result = id
result = 31 * result + input0.hashCode()
result = 31 * result + inputImage.contentHashCode()
return result
}

然后→

data class FeatureXXState(
val initialStateInput0:String = "",
val initialStateImage:ByteArray = ByteArray(0)
){
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as F3TextFieldState
if (initialStateInput0 != other.initialStateInput0) return false
if (!initialStateImage.contentEquals(other.initialStateImage)) return false
return true
}
override fun hashCode(): Int {
var result = initialStateInput0.hashCode() 
result = 31 * result + initialStateImage.contentHashCode()
return result
}

最终→

sealed class ModifyFeatureXXEvent{
data class EnteredValueInput0(val value0:String):ModifyFeatureXXEvent()
data class EnteredImageValue(val imgValue:ByteArray):ModifyFeatureXXEvent(){
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as EnteredImageValue
if (!imgValue.contentEquals(other.imgValue)) return false
return true
}
override fun hashCode(): Int {
return imgValue.contentHashCode()
}    
}

没有更改视图模型。