解决ViMusic崩溃难题:从异常捕获到用户体验优化
原创 于 2025-09-08 05:49:02 发布 · 825 阅读 ·
CC 4.0 BY-SA版权
when (error?.cause?.cause) {
    is UnresolvedAddressException, is UnknownHostException -> 
        "A network error has occurred"
    is IOException -> 
        "A connection error has occurred"
    else -> 
        "An unknown playback error has occurred"
}
text = "An error has occurred while fetching the ${
    if (isShowingSynchronizedLyrics) "synchronized " else ""
}lyrics"
relatedPageResult?.exceptionOrNull()?.let {
    ErrorMessage(
        text = "An error has occurred",
        action = { retry() }
    )
}
dependencies {
    classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.9'
}
plugins {
    id 'com.google.firebase.crashlytics'
}

dependencies {
    implementation 'com.google.firebase:firebase-crashlytics-ktx:18.4.0'
}
class MainApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true)
        
        Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
            FirebaseCrashlytics.getInstance().recordException(throwable)
            // 保留原有异常处理逻辑
            defaultExceptionHandler.uncaughtException(thread, throwable)
        }
    }
}
try {
    val videoId = dataSpec.key ?: error("A key must be set")
} catch (e: Exception) {
    FirebaseCrashlytics.getInstance().apply {
        setCustomKey("video_id", dataSpec.key ?: "unknown")
        setCustomKey("media_type", "audio")
        recordException(e)
    }
    throw e
}