如何调试Android项目

发布于 2024-12-07 05:07:29 字数 522 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

几味少女 2024-12-14 05:07:29

如果你想调试:
右键单击要调试的代码部分的左侧(在行号处)。选择切换断点。

在此处输入图片描述

可以看到行号旁边有一个小圆圈,那就是断点。

在此处输入图像描述

然后,进行运行配置,单击运行图标旁边的小箭头,然后单击“运行配置..”

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

enter image description here

You can see small circle beside the line number, that is the breakpoint.

enter image description here

Then, make run configuration, click small arrow beside run icon, and click 'run configurations..'

enter image description here

Click on android application, you will see this dialog window:

enter image description here

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:

enter image description here

fill it, browse Your project,

enter image description here

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,

enter image description here

for the Common tab, if maybe You want to check Display in favorites menu for 'Debug' or 'Run' option. It is optional. Click apply.

enter image description here

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.

enter image description here

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.

enter image description here

If eclipse give confirmation to open perspective, select Yes

enter image description here

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.

enter image description here

清晰传感 2024-12-14 05:07:29

使用 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

怎会甘心 2024-12-14 05:07:29

我强烈推荐这个关于一般调试的教程。

http://www.vogella.com/articles/EclipseDebugging/article.html

它会带您了解 Eclipse 在该部门提供的所有功能,并且非常值得您花时间。

至于可能有帮助的特定技巧,设置断点可能很有用(即,单击代码的左边距以显示其中一个蓝色引脚)。然后,在调试中运行时,您可以在问题发生的地方附近停下来,然后使用“step into”、“stepforward”和“step return”命令一步步前进。调试窗口的顶部。当您以这种方式单步执行代码时,您可能会打开变量窗口,这样您就可以看到以应有的方式设置了哪些内容,等等。

或者,您可以在 RuntimeException 上设置一个断点(即使是捕获),通过单击调试中的“断点”窗口,然后单击“J!”按钮(鼠标悬停文本显示“添加 Java 异常断点”。

调试视图还允许您执行其他很酷的操作,例如:

  • 让您设置“观察点”,以便您可以看到应用程序何时更改相关字段等。
  • “拖放到框架” ”这样你就可以在中间启动你的应用程序。
  • 设置步骤过滤器,这样可以更有效地完成我前面提到的“单步执行”。
  • 创建一个详细格式化程序,一小段 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:

  • letting you set "watchpoints", so you can see when the application changes relevant fields, etc.
  • "Drop to frame" so you can start your app in the middle.
  • Set up step filters, so that "stepping" through I mentioned earlier can be done more efficiently.
  • Create a detail formatter, a little snippet of java code that formats the output of a field so you can view it more cleanly in the debugger.
  • And much more!

Again, I recommend the above-referenced tutorial. And welcome to the wonderful world of debugging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文