【问题标题】:Android App crashes without any Stack Trace ErrorAndroid 应用程序崩溃而没有任何堆栈跟踪错误
【发布时间】:2019-07-26 00:11:18
【问题描述】:

我刚开始使用那个新应用程序,想看看我是否通过 Retrofit 得到任何响应并将其打印到 TextView 中。

但是应用程序在没有任何堆栈跟踪的情况下崩溃,所以没有异常......什么都没有。

我有一个 Retrofit 接口和一个“工厂”,它创建请求,一切都通过 Kotlin 协程在不同的线程中运行。

    class MainActivity : AppCompatActivity() {

    private lateinit var debugTextView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        debugTextView = findViewById(R.id.debugTextView)

        val service = RetrofitFactory.makeCarDataService()
        GlobalScope.launch(Dispatchers.Main) {
            val request = service.getData()

            val response = request.await()

            debugTextView.text =response.toString()

        }
    }
}

我很想发布一个错误...但是没有,一切都应该完美运行:/

【问题讨论】:

  • 不可能没有stacktace...
  • 只是启动应用程序和调试器的堆栈跟踪,但应用程序立即崩溃而没有错误/异常...
  • 删除debugTextView = findViewById(R.id.debugTextView)之后的所有代码,是不是崩溃了?

标签: android kotlin retrofit2 kotlin-coroutines


【解决方案1】:

要使用Dispatchers.Main,我们需要将以下行添加到应用的build.gradle 文件依赖项中:

实现 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'

没有这种依赖关系可能是应用崩溃而没有任何堆栈跟踪的原因。

【讨论】:

  • 这对我有用。像这样删除 $ 符号: implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
【解决方案2】:
尝试这个:
try {
    val request = service.getData()
    val response = request.await()
    debugTextView.text =response.toString()
} catch (e: RuntimeException) {
    e.printStackTrace()
} catch (t: Throwable) {
    t.printStackTrace()
}

【讨论】:

    【解决方案3】:

    我对协程不是很熟悉,但是您是否尝试过仅在视图上设置文本时才使用主线程?像这样:

    val service = RetrofitFactory.makeCarDataService()
        GlobalScope.launch(Dispatchers.Default/*change here*/) {
            val request = service.getData()
    
            val response = request.await()
            /*change here*/
            withContext(Dispatchers.Main) {
                debugTextView.text = response.toString()
            }
    
        }
    }
    

    【讨论】:

      【解决方案4】:

      如果没有堆栈跟踪,这些是可能的问题:

      service.getData()
      

      可以抛出NullPointerException

      debugTextView.text
      

      可以抛出NullPointerException

      response.toString()
      

      可以抛出NullPointerException

      【讨论】:

        猜你喜欢
        • 2012-06-08
        • 1970-01-01
        • 2015-09-11
        • 2011-06-16
        • 1970-01-01
        • 1970-01-01
        • 2020-12-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多