Android StateFlow收集发出NullPointerException
我的存储库层有一个Android StateFlow收集发出NullPointerException,android,kotlin,kotlin-coroutines,kotlin-flow,Android,Kotlin,Kotlin Coroutines,Kotlin Flow,我的存储库层有一个MutableStateFlow,在我的ViewModel中收集它。我在一些用户设备上得到这个NPE Fatal Exception: java.lang.NullPointerException at a.b.c.ui.viewmodel.HomeViewModel$collectFlowState$$inlined$collect$1.emit(HomeViewModel.java:189) at a.b.c.ui.viewmodel.HomeV
MutableStateFlow
,在我的ViewModel中收集它。我在一些用户设备上得到这个NPE
Fatal Exception: java.lang.NullPointerException
at a.b.c.ui.viewmodel.HomeViewModel$collectFlowState$$inlined$collect$1.emit(HomeViewModel.java:189)
at a.b.c.ui.viewmodel.HomeViewModel$collectFlowState$$inlined$collect$1$1.invokeSuspend(HomeViewModel.java:12)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:33)
at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTaskKt.java:176)
at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTaskKt.java:111)
at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.java:308)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.java:318)
at kotlinx.coroutines.CancellableContinuationImpl.resumeUndispatched(CancellableContinuationImpl.java:400)
at kotlinx.coroutines.android.HandlerContext$scheduleResumeAfterDelay$$inlined$Runnable$1.run(HandlerContext.java:19)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7830)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1040)
MutableStateFlow
是非空数据,如果数据以某种方式为空,则应用程序会更早崩溃
我如何在存储库(生产者)层上使用StateFlow
的示例:
ViewModel(消费者)端:
从
状态流永远不会完成。状态流上对Flow.collect的调用永远不会正常完成,由Flow.launchIn函数启动的协同路由也不会正常完成
并且建议捕获这样的异常
try {
flow.collect { value ->
println("Received $value")
}
} catch (e: Exception) {
println("The flow has thrown an exception: $e")
}
if (viewModel.mutableStateFlow.value == YourInitValue) {
lifecycleScope.launch {
viewModel.mutableStateFlow.timeLineData.collect {
}
}
}
因此,建议吞掉StateFlow
的collect
中的所有异常,还是仅吞掉生产者端抛出的异常?
有人能帮我理解NPE的一般原因吗
谢谢我想这可以解决你的问题
private val dataSF = MutableStateFlow<Int?>(someState)
private val dataSF=MutableStateFlow(someState)
我遇到了同样的问题,但在重新创建片段时发生了这种情况,因为Stateflow仍然有一个旧值,所以只需在调用flow.collect
之前添加一个check,check for flow包含类似这样的init值
try {
flow.collect { value ->
println("Received $value")
}
} catch (e: Exception) {
println("The flow has thrown an exception: $e")
}
if (viewModel.mutableStateFlow.value == YourInitValue) {
lifecycleScope.launch {
viewModel.mutableStateFlow.timeLineData.collect {
}
}
}
因为如果stateflow不包含init值,这意味着之前调用的collect方法,请向我们展示您的
MutableStateFlow
实现,它发出这些值。Hi@ChristianB已经做了一些更改。请再看一眼。谢谢你也能给我们看看你的HomeViewModel吗?添加了@hamid谢谢!stacktrace看起来更像是您的HomeViewModel
中的collect{…}
块中的问题。你能发布更多关于那里发生了什么的细节吗?我的流量总是有一些价值的。不要想引入不必要的可空值。我认为这不是设备问题,是否有任何特定的场景来重现崩溃?