Android应用程序崩溃没有任何堆栈跟踪错误

3

我刚开始使用这个新的应用程序,想看看是否可以通过Retrofit获取任何响应并在TextView中打印它。

但是应用程序崩溃了,没有任何堆栈跟踪,因此没有异常...什么也没有。

我有一个Retrofit接口和一个“工厂”,用于创建请求,所有内容都在不同的线程中运行,通过Kotlin Coroutine实现。

    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()

        }
    }
}

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

不可能,没有堆栈跟踪... - John Joe
应用程序启动时出现堆栈跟踪和调试器,但应用程序立即崩溃,没有错误/异常信息... - Strohhut
删除 debugTextView = findViewById(R.id.debugTextView) 之后的所有代码,程序崩溃了吗? - John Joe
4个回答

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

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'

缺少该依赖可能是应用程序崩溃而没有任何堆栈跟踪的原因。

这对我有用。不过要去掉$符号,像这样:implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1' - NezSpencer

0

没有堆栈跟踪,可能存在以下问题:

service.getData()

可能会抛出NullPointerException异常

debugTextView.text

可能会抛出NullPointerException异常

response.toString()

可以抛出 NullPointerException

0

我对协程不是很熟悉,但你尝试过只在主线程上设置视图文本吗?就像这样:

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()
        }

    }
}

-1
试试这个:
try {
    val request = service.getData()
    val response = request.await()
    debugTextView.text =response.toString()
} catch (e: RuntimeException) {
    e.printStackTrace()
} catch (t: Throwable) {
    t.printStackTrace()
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接