Android Studio调试器突出显示错误的行 [英] Android Studio debugger highlights the wrong lines
问题描述
我看不到有什么功能上的错误在执行什么,但我只是浪费了一个小时,证明我的代码,编译器正在做他们应该做的。
I can't see anything functionally wrong in terms of what gets executed, but I've just wasted an hour proving my code and the compiler are doing what they're supposed to.
考虑以下代码:
public int getAnswer(int a) {
int retval = 18;
int x = 25;
int y = 50;
while (a-- > 0) {
if (a == 3) {
retval = a;
return retval;
}
}
x = 10;
y = 20;
return 0;
}
使用
int theAnswer = getAnswer(6);
断点包含返回
的行在运行代码之前,请尝试并预测结果,因此将会触发哪一行。
Breakpoint both of the lines containing return
and before you run the code, try and predict the result, and therefore which line will be hit.
正如你可能从我的问题的语气所想象的那样,错误的一行是突出显示,但返回正确的结果。如果你通过代码,它跳舞了很多。
As you might imagine from the tone of my question, the wrong line is highlighted, but the right result is returned. If you step through the code, it dances around quite a bit.
有没有什么可以配置不同,以便显示正确的行? (我正在使用最新的Android Studio,我感激的是潜在的不稳定,但我确信我已经看到Eclipse的类似行为,但我从来没有花时间跟踪它;也许这是一个java的东西)
Is there anything that can be configured differently such that the correct lines are displayed? (I'm using the latest Android Studio, which I appreciate is potentially unstable, but I'm sure I've seen similar behaviour on Eclipse a while back but I never spent the time then tracking it down; perhaps it's a java thing).
推荐答案
这是一个dx的问题,它是构建的一部分,将您的Java .class文件转换为.dx用于打包到Android的文件。根据这个:
This is a problem with dx, which is the part of the build that turns your Java .class files into .dx files for packaging into Android. According to this:
https://code.google.com/p/android/issues/detail?id=34193
如果函数有多个返回路径, dx将返回指令合并到单个返回指令中,因此在调试期间,调试器无法知道返回属于哪个行,事情会跳转。这对应于当我尝试重现你的问题时看到的:每次循环它都会执行 if(a == 3)
检查,跳转到 return 0
在结尾,然后跳回到循环。你看到最后一个返回0
与循环中间的 return retval
合并。
if a function has multiple return paths, dx merges the return instruction into a single return instruction, so during debugging, the debugger can't tell which line a return belongs to and things jump around. This corresponds to what I see when I try to reproduce your problem: each time through the loop it does the if (a == 3)
check, jumps to the return 0
at the end, and then jumps back into the loop. You're seeing that last return 0
get merged with the return retval
in the middle of the loop.
我怀疑这将很快就会修复,所以你可能只需要学会与之一起生活。对不起,我知道这很疯狂。
I doubt this will be fixed any time soon, so you may just have to learn to live with it. Sorry, I know it's kinda crazy.
这篇关于Android Studio调试器突出显示错误的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!