对象评估在Android Studio调试中始终为无效

发布于 2025-01-25 17:33:18 字数 788 浏览 1 评论 0原文

在Android Studio调试时,我发现了一种奇怪的行为。我正在使用真实的设备(不是模拟器)。我有以下代码:

 val currentFragment = supportFragmentManager.findFragmentById(R.id.act_navhost_fragment)
            if (currentFragment is BottomNavFragment) {
            ...

在调试中,我看到以下内容:

如您所见,电流范围不是null。但是,如果我尝试仅评估表达式 supportFragmentManager.findFragmentById(r.id.act_navhost_fragment),结果为null。

怎么可能?我知道这可能是一个非常愚蠢的问题,但是我不明白为什么评估表达与运行时代码相比,会产生不同的结果,也许是因为片段ID?有没有办法评估这种表达方式?

I have found a weird behavior while debugging in Android Studio. I am using a real device (not an emulator). I have the following code:

 val currentFragment = supportFragmentManager.findFragmentById(R.id.act_navhost_fragment)
            if (currentFragment is BottomNavFragment) {
            ...

In debug I see the following:
enter image description here

As you may see, currentFragment is not null. However, if I try to evaluate only the expression
supportFragmentManager.findFragmentById(R.id.act_navhost_fragment), the result is null.

enter image description here

How is it possible? I know it's probably a very silly question but I don't understand why evaluate expression gives a different result compare to runtime code, maybe it's because of fragment id? Is there a way to evaluate such expressions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2025-02-01 17:33:18

It seems it's a kotlin bug, R values are resolved incorrectly.

https://youtrack.jetbrains.com/issue/KTIJ-18232

Workaround I have found is to copy the correct R.id.* int value (eg printing a log or directly from R file) and replace the R.id.* with the copied int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文