首页 > 解决方案 > 如何从 DaggerApplication 中的 UncaughtExceptionHandler 启动活动

问题描述

我正面临着类似的情况具有相同的奇怪行为:意图不会触发,崩溃对话框也不会显示,并且应用程序在崩溃后自行关闭。Logcat显示如下错误:

E/InputEventSender: Exception dispatching finished signal.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.NumberFormatException: For input string: "7512186516213"
and so on....
D/Android Runtime: Shutting down VM

我们如何从UncaughtExceptionHandlerin开始活动DaggerApplication

这是我的代码。我在applicationContext到处使用,我已经在我的文件中注册了ExceptionDisplay活动。Manifest我确信他们DaggerApplication可以像Application上课一样处理这个问题,但不幸的是我自己无法解决它。

class IpunktApp : DaggerApplication() {

    override fun applicationInjector(): AndroidInjector<out DaggerApplication> =
        DaggerAppComponent.factory().create(this)

    private lateinit var defaultUEH: Thread.UncaughtExceptionHandler

    private val unCaughtExceptionHandler = Thread.UncaughtExceptionHandler { thread, ex ->
        val stringWriter = StringWriter()
        ex?.printStackTrace(PrintWriter(stringWriter))
        val exception = stringWriter.toString()
        saveLogs(exception.toByteArray(), applicationContext)

        applicationContext.startActivity(Intent(Intent(applicationContext, ExceptionDisplay::class.java).apply {
            putExtra("error", exception)
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }))

        defaultUEH.uncaughtException(thread, ex)
        exitProcess(1)
    }


    override fun onCreate() {
        super.onCreate()
        setupExceptionHandler()
    }


    private fun setupExceptionHandler() {
        try {
            defaultUEH = Thread.getDefaultUncaughtExceptionHandler()
            Thread.setDefaultUncaughtExceptionHandler(unCaughtExceptionHandler)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

任何建议表示赞赏。提前致谢。

标签: androidkotlinandroid-intentdagger-2uncaughtexceptionhandler

解决方案


推荐阅读