Android Studio 调试器突出显示错误的行 [英] Android Studio debugger highlights the wrong lines

查看:33
本文介绍了Android Studio 调试器突出显示错误的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就执行的内容而言,我看不出任何功能上的错误,但我只是浪费了一个小时来证明我的代码和编译器正在做他们应该做的事情.

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

断点包含 return 的两行,在运行代码之前,尝试预测结果,从而预测将命中哪一行.

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/问题/详细信息?id=34193

如果一个函数有多个返回路径,dx 会将返回指令合并为一条返回指令,所以在调试过程中,调试器无法分辨返回属于哪一行,事情会跳来跳去.这对应于我尝试重现您的问题时看到的内容:每次通过循环时,它都会执行 if (a == 3) 检查,跳转到 return 0最后,然后跳回循环.您会看到最后一个 return 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆