gpt4 book ai didi

Android Kotlin Coroutine 在 Logcat 中没有报告 fatal error 的情况下崩溃

转载 作者:行者123 更新时间:2023-11-30 04:55:52 25 4
gpt4 key购买 nike

我的代码中反复出现崩溃,似乎是无缘无故开始发生的。我的代码编译正确,然后应用程序在调用 viewModel 后立即崩溃,并且没有在 logcat 中报告错误!!第一次看到logcat不报错的crash。通过使用 Timber 语句,我能够确定代码崩溃发生在实时数据更新之前的 withContext 行上。我的问题是:如何才能看到 fatal error ,以便知道如何解决崩溃问题?

class DashboardViewModel : ViewModel() {
private var delDate = ""
private var thread: Job = Job()
private var duration: Int = ConfigData.refresh * 1000
private var counter: Int = 0
private var area: String = "All"
var estFinishLiveData: MutableLiveData<String> = MutableLiveData()
var trailingAreaLiveData: MutableLiveData<AreaModel> = MutableLiveData()
var percentCompleteLiveData: MutableLiveData<OverallModel> = MutableLiveData()
var chartDataLiveData: MutableLiveData<ArrayList<ChartModel>> = MutableLiveData()

fun loadDataRefresher(periodic: Boolean, selectedArea: String) {
counter++
area = if (selectedArea == "All") "" else selectedArea
Timber.d("Dashboard update $counter for $selectedArea next in ${ConfigData.refresh} seconds")
thread = CoroutineScope(Dispatchers.IO).launch {
loadDelDate()
loadEstimatedFinish()
loadTrailingArea()
loadPercentComplete()
loadChartData()
if (periodic) {
delay(duration.toLong())
loadDataRefresher(false, selectedArea)
}
}
}

private fun loadDelDate() {
delDate = DatabaseMgr.spRetrieveDeliveryDate("")
}

private suspend fun loadEstimatedFinish() {
val estFinish = DatabaseMgr.spRetrieveEstimatedFinish(delDate)
withContext(Dispatchers.Main) {
estFinishLiveData.value = estFinish
}
}

private suspend fun loadTrailingArea() {
val areaModel = DatabaseMgr.spRetrieveTrailingArea(delDate)
if (areaModel.description == "") {
areaModel.description = "None in progress"
}
withContext(Dispatchers.Main) {
trailingAreaLiveData.value = areaModel
}
}

最佳答案

我找到了自己的解决方案。显然,更新后的 Android jetpack 核心库与我使用的来自 jetbrains 的协程库之间存在冲突。一旦我从 jetbrains 中删除了库,协程就可以完美运行。

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
// Testing
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// // Coroutines
// implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0" <- Causes a clash with jetpack's core libary
// implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
// Dagger 2
implementation "com.google.dagger:dagger:2.24"
implementation "com.google.dagger:dagger-android:2.24"
kapt "com.google.dagger:dagger-compiler:2.24"
kapt "com.google.dagger:dagger-android-processor:2.24"
// View Model
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0'
}

关于Android Kotlin Coroutine 在 Logcat 中没有报告 fatal error 的情况下崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59184029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com