简体   繁体   中英

How to debug Android project

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

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

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.

在此处输入图片说明

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

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 (ie, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM