热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AndroidStudio调试器突出显示了错误的行-AndroidStudiodebuggerhighlightsthewronglines

Icantseeanythingfunctionallywrongintermsofwhatgetsexecuted,butIvejustwastedanhour

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.

我看不出在执行什么方面有什么功能上的错误,但我只是浪费了一个小时来证明我的代码和编译器正在做它们应该做的事情。

Consider this code:

考虑这段代码:

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

Called with

调用了

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.

正如您可能从我的问题的语气中想象的那样,错误的行被突出显示,但是返回正确的结果。如果您一步一步地完成代码,它就会跳来跳去。

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

是否有任何东西可以以不同的方式配置以显示正确的行?(我正在使用最新的Android Studio,我认为它可能不稳定,但我确信我以前在Eclipse上看到过类似的行为,但我从来没有花时间跟踪它;也许是java的东西)。

3 个解决方案

#1


42  

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:

这是dx的一个问题,它是构建的一部分,将Java .class文件转换成。dx文件,用于打包到Android。根据这个:

https://code.google.com/p/android/issues/detail?id=34193

https://code.google.com/p/android/issues/detail?id=34193

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.

如果一个函数有多个返回路径,dx将返回指令合并到一个返回指令中,因此调试期间,调试器无法知道返回是属于哪一行的,事情就会跳来跳去。这与我尝试重现您的问题时看到的情况一致:每次循环执行if (a == 3)检查时,在结束时跳转到return 0,然后跳转回循环。您将看到最后一个return 0与循环中间的return retval合并。

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.

我怀疑这个问题不会很快得到解决,所以你可能得学着适应它。对不起,我知道这有点疯狂。

#2


2  

Try Build -> Rebuild project and then Shift + F9. You have outdated code running on application.

尝试Build ->重建项目,然后Shift + F9。在应用程序上运行过时的代码。

#3


2  

Here's what worked for me, at least for stepping into Android SDK source code:

以下是对我有用的东西,至少对于进入Android SDK源代码来说是这样的:

  1. Install the SDK Platform and Sources for Android SDK for exactly the API level of the version of Android you are running during development (use the Android version history page to find out which API level your device has).
  2. 为Android SDK安装SDK平台和源代码,以获得在开发过程中运行的Android版本的API级别(使用Android版本历史页面来查找设备的API级别)。
  3. Compile your app for exactly that version. For example, in Android Studio, open your app's build.gradle file and set the value of compileSdkVersion accordingly.
  4. 为这个版本编译你的应用程序。例如,在Android Studio中,打开应用程序的构建。渐变文件,并设置相应的编译dkversion的值。
  5. Debug your app. When you step into Android SDK source, you should see the correct line.
  6. 调试你的应用程序。当你进入Android SDK源代码时,你应该看到正确的路线。
  7. (optional) Recompile with the latest SDK before deploying. In other words, change compileSdkVersion back.
  8. (可选)部署前使用最新的SDK重新编译。换句话说,更改编译回dkversion。

推荐阅读
author-avatar
kkka姐姐_244
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有