如何调试Android项目
我是 Android 框架(使用 Eclipse)的新手,我有一个关于调试的问题。最近我在一个活动中犯了这个错误:
// The installation button
Button button = (Button) findViewById(R.id.preparationInstallButtonID);
button.setOnClickListener(this);
setContentView(R.layout.preparation);
即我尝试在渲染布局之前添加一个 onclick 事件。这导致程序崩溃,“抱歉,应用程序 xxx 意外停止”。在调试模式下再次运行它,Eclipse 将打开一个包含“Thread<3>(挂起)”等的调试窗口(我确信您知道我在谈论哪个窗口)。
现在,我如何能够在这里提取有用的数据?关于上面的问题,我找到了如何通过试错来修复它,但是假设我无法进行试错,调试窗口如何帮助我找到错误的根源?它说“异常 RuntimeException”,但我没有看到任何关于我的代码中发生异常的指针。
I'm new to the Android framework (using Eclipse) and I have a question about debugging. Recently I did this error in an activity:
// The installation button
Button button = (Button) findViewById(R.id.preparationInstallButtonID);
button.setOnClickListener(this);
setContentView(R.layout.preparation);
i.e. I tried to add an onclick-event prior the layout had been rendered. This crashed the program, "Sorry, the application xxx has stopped unexpectedly". Running it again, with debug-mode, Eclipse opens a Debug-window containing "Thread<3> (Suspended)" etc (I'm sure you know which window I'm talking about).
Now, how am I able to extract useful data here? Regarding the problem above I found how to fix it by trial-and-erroring, but let's say I'm not able to do trial-and-error, how can the debug-window help me find the source of the error? It says "exception RuntimeException", but I don't see any pointers as of where in my code the exception occured.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想调试:
右键单击要调试的代码部分的左侧(在行号处)。选择切换断点。
可以看到行号旁边有一个小圆圈,那就是断点。
然后,进行运行配置,单击运行图标旁边的小箭头,然后单击“运行配置..”
< img src="https://i.sstatic.net/hmsxQ.png" alt="在此处输入图像描述">
单击 Android 应用程序,您将看到以下对话框窗口:
阅读它,“突出显示”此内容:“按新按钮创建所选类型的配置”。单击选项卡上方的“新建”按钮,您将看到此对话框:
填写它,浏览您的项目,
例如,我添加 Scrumptious 项目,并启动默认活动。单击“目标”选项卡,选择“您的模拟器”。如果您想在物理设备上运行,请单击“始终提示选择设备”。这次我在物理设备上运行,
用于“公共”选项卡,如果您想检查“在收藏夹中显示” “调试”或“运行”选项的菜单。它是可选的。单击“应用”。
单击调试图标 - 并选择您的项目。如果您在收藏夹菜单中的“显示”上激活“调试”,则该项目将显示在调试选项菜单中。
我选择“始终提示选择设备”,将显示此对话框。我想在物理设备上运行,因此我在“选择正在运行的 Android 设备”处单击该设备,然后单击“确定”。也可以在模拟器上运行。只需单击您的模拟器,然后单击“确定”。
如果 Eclipse 确认打开透视图,请选择“是”
然后,如果您想知道变量的值是什么,只需将鼠标放在变量的实例上,或者您可以在“变量”窗口中浏览调试视角。
If You want to debug:
Right click left of Your part of code You want to debug (at line number). select Toggle Breakpoint.
You can see small circle beside the line number, that is the breakpoint.
Then, make run configuration, click small arrow beside run icon, and click 'run configurations..'
Click on android application, you will see this dialog window:
Read it, "highlight" this: 'Press new button to create a configuration of the selected type'. Click the New button, above the tab, You will see this dialog:
fill it, browse Your project,
for example, I add Scrumptious project, and launch default activity. Click on Target Tab, choose Your emulator. If You want to run on physical device, click 'Always prompt to pick device'. I run on physical device this time,
for the Common tab, if maybe You want to check Display in favorites menu for 'Debug' or 'Run' option. It is optional. Click apply.
click debug icon - and select your project. If You activate 'Debug' on Display in favorites menu, the project will shown at the debug option menu.
I choose 'Always prompt to pick device', this dialog will shown. I want to run at physical device, so I click the device at the 'Choose a running Android Device', and click 'Ok'. You can also run on emulator. Just click your emulator, and click Ok.
If eclipse give confirmation to open perspective, select Yes
Then, if You want to know what is value of the variable, just put your mouse to the instance of variable, or You can browse at 'Variables' window at Debug perspective.
使用 LogCat,这将为您提供错误消息以及测试设备/模拟器中发生的所有其他信息。
窗口->显示视图->其他->日志猫
Use the LogCat this will give you the error messages and everything else that goes on in your testdevice/emulator.
Window -> Show View -> Other -> LogCat
我强烈推荐这个关于一般调试的教程。
http://www.vogella.com/articles/EclipseDebugging/article.html
它会带您了解 Eclipse 在该部门提供的所有功能,并且非常值得您花时间。
至于可能有帮助的特定技巧,设置断点可能很有用(即,单击代码的左边距以显示其中一个蓝色引脚)。然后,在调试中运行时,您可以在问题发生的地方附近停下来,然后使用“step into”、“stepforward”和“step return”命令一步步前进。调试窗口的顶部。当您以这种方式单步执行代码时,您可能会打开变量窗口,这样您就可以看到以应有的方式设置了哪些内容,等等。
或者,您可以在 RuntimeException 上设置一个断点(即使是捕获),通过单击调试中的“断点”窗口,然后单击“J!”按钮(鼠标悬停文本显示“添加 Java 异常断点”。
调试视图还允许您执行其他很酷的操作,例如:
我再次推荐上述教程,欢迎来到调试的奇妙世界。
I strongly recommend this tutorial on debugging in general.
http://www.vogella.com/articles/EclipseDebugging/article.html
It takes you through all the features Eclipse offers in that department and is well worth the time.
As far as the particular trick that might've helped, it might have been useful to set a breakpoint (i.e., clicking in the left margin of the code to make one of those blue pins appear). Then, when running in debug, you could stop near where the problem occurred, and step forward, beat by beat, using the "step into," "step forward," and "step return" commands at the top of debug window. While you're stepping around the code in that way, you might have the variables window open, so you could see what things are set in a manner they should be, etc.
Alternatively, you could set a breakpoint on RuntimeException (even one that's caught), by clicking on the Breakpoints window in debug, and then licking the "J!" button (the mouseover text shows "add Java Exception Breakpoint."
The debug view lets you do other cool stuff too, like:
Again, I recommend the above-referenced tutorial. And welcome to the wonderful world of debugging.