【问题标题】:Why Android Studio debugger doesn't stop on code that is called inside runOnUiThread为什么 Android Studio 调试器不会停止在 runOnUiThread 内部调用的代码上
【发布时间】:2021-03-15 14:23:17
【问题描述】:

我在以下代码中发现了这个错误:

(it.context.get() as? Activity)?.runOnUiThread(Runnable {
            it.weakRefIOnDataRefreshed.get()?.onDataRefreshed(refreshedItemList)
        })

上面的代码在一个在非 UI 线程中运行的 run 方法中。我想以 refreshedItemList 作为参数调用片段中的方法。我想调试 onDataRefreshed 方法(在 Fragment 内),我在这个函数内放置了一个断点,但没有任何反应。我还放置了一个 Log 方法来确保代码正在运行并且 Log 方法确实可以打印。调试器没有停在我标记的行上可能是什么问题?

【问题讨论】:

标签: java android android-studio kotlin debugging


【解决方案1】:

在后台线程工作的类中定义一个 Handler 对象并使用 post 方法更新 UI,例如(Java)。

// Define handler object
Handler handler = new Handler(Looper.getMainLooper);
...
...

// Here ise where you want to update your UI
handler.post(() -> {
    // Your code with which you want to update the UI
}

【讨论】:

  • 我这样做有什么问题吗?为什么调试器会忽略我的请求?
  • 这完全取决于您实现代码的方式。只看两行代码很难说什么。我对你的代码一无所知,你说出了问题,但没有分享足够的代码来分析。
  • 在两次重新启动后突然感谢它工作:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多