对象评估在Android Studio调试中始终为无效
在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) {
...
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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来这是一个kotlin错误,R值无法正确解决。
https://youtrack.jetbrains.com/issue/issue/ktij-18232 发现的是复制正确的r.id。
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.