【发布时间】:2019-10-04 16:30:30
【问题描述】:
例如,当调用 RxJava 映射运算符后发生 NullPointerException 时,应用程序不会崩溃。我希望应用程序在发生这种情况时崩溃,以便它可以向 crashlytics 等提交报告。
我尝试在 try/catch 块中使用 Exceptions.propagate(),但没有奏效。
我找到的唯一解决方案是在我的错误处理程序中抛出 RuntimeException。
override fun getCategories(): Single<List<Category>> {
return ApiManager
.getCategoriesService()
.getCategories()
.map { categoriesResponse ->
throw KotlinNullPointerException
categoriesResponse.categories?.map { categoryResponse ->
CategoryMapper().mapFromRemote(categoryResponse)
}
}
}
在 map 运算符中抛出的 NullPointerException 不会使应用程序崩溃。 如果我在 return 语句之前调用它,它会使应用程序崩溃。
【问题讨论】:
-
谁订阅了这个,如何订阅?您可能会在订阅的可抛出区域捕获 NPE,它基本上像“try/catch”一样运行,导致它不会崩溃
-
另外,你为什么要让应用程序崩溃?对于 rxjava 的意图,您可能没有一个好的用例(即尽一切可能不崩溃)
-
这个我被域层中的 UseCase 调用,而后者又从 ViewModel 调用,使用
.subscribeOn(Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR)) .observeOn(AndroidSchedulers.mainThread())。主要用于开发目的,当mapper出现问题时,p.e NullPointerException,它不会显示在logcat中。 -
> 您很可能在订阅的可抛出区域捕获了 NPE,它基本上像“try/catch”一样运行,导致它不会崩溃 我该如何规避这个问题?例如,我希望在 logcat 上显示错误,这样我就可以了解 Mapper 中的问题出在哪里
标签: android kotlin rx-java2 rx-android