0

我实现了一个异常处理程序来捕获应用程序上的所有异常。

class ExceptionHandler : Thread.UncaughtExceptionHandler {
    private val LINE_SEPARATOR = "\n"
    override fun uncaughtException(thread: Thread, exception: Throwable) {
        val stackTrace = StringWriter()
        exception.printStackTrace(PrintWriter(stackTrace))
        val errorReport = StringBuilder()
        errorReport.append("************ CAUSE OF ERROR ************\n\n")
        errorReport.append(stackTrace.toString())
        // TODO: Report log
    }
}

我得到了如下的 NPE 报告。但我不知道它是什么,因为它被混淆了

java.lang.NullPointerException
    at l.d(java-style lambda group:2)
    at androidx.lifecycle.LiveData.b(LiveData.java:6)
    at androidx.lifecycle.LiveData.c(LiveData.java:8)
    at x.r.x.j(MutableLiveData.java:4)
    at com.example.ui.ExampleViewModel$b.i(ExampleViewModel.kt:14)
    at c0.k.j.a.a.g(ContinuationImpl.kt:3)
    at v.a.g0.run(DispatchedTask.kt:15)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6746)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

ExampleViewModel 中报错的那一行是这样的:

fun callAPI(requestBody: MyRequestBody): Job = viewModelScope.launch {
    try {
        val response = repository.fetchAPI(requestBody)

        if (response.isSuccessful) {
                _responseBody.value = response.body()
        } else {
            val msg = "callAPI() ${response.code()}: ${response.message()} ${response.errorBody()!!.string()}"
            when {
                response.code() >= 500 -> {
                    _toastMsg.value = msg
                }
            }
        }
    } catch (e: java.lang.Exception) {
        _toastMsg.value = "${e.toString()}"
    }
}

该函数使用一个参数,如下所示:

@Parcelize
@Keep
data class MyRequestBody(
        val aData: String,
        val bData: String,
        val cData: String,
        val dData: String,
        val eData: String,
        val fData: String,
        val gData: Int,
        val hData: String?,
        val iData: String?,
        val jData: String?,
        val kData: String?
):Parcelable{
    override fun toString(): String {
        return "${aData}, ${bData}, ${cData}..."
    }
}

如何映射数据以便我可以看到它们的原始名称?哪一部分导致NPE?(我猜响应是空的?)我不知道如何映射到应用程序以及 LiveData、MutableLiveData。

4

0 回答 0