Debugging Your Android Application in the Emulator


Build your own Android App Dev Empire

Get Instant Access

Before we go any further, you need to become familiar with debugging in the emulator. To illustrate some useful debugging tools, let's manufacture an error in the My First Android Application.

In your project, edit the file and create a new method called forceError() in your class and make a call to this method in your onCreate() method. The forceError() method forces a new unhandled error in your application. The forceError() method should look something like this:

public void forceError() { if(true) {

throw new Error("Whoops");

Eclipse has perspectives (each a set of specific panes) for coding and debugging. You can switch between perspectives by choosing the appropriate name in the top-right corner of the Eclipse environment. The Java perspective arranges the appropriate panes for coding and navigating around the project. The Debug perspective enables you to set breakpoints, view LogCat information, and debug. The Dalvik Debug Monitor Service (DDMS) perspective enables you to monitor and manipulate emulator and device status.

It's probably helpful at this point to run the application and watch what happens. Do this using the Run Configuration first. In the emulator, you see that the application has stopped unexpectedly. You are prompted by a dialog that enables you to forcefully close the application, as shown in Figure 3.12.

Shut down the application and the emulator. Now it's time to debug.You can debug the MyFirstAndroidApp application using the following steps:

1. Choose the Debug As icon drop-down menu on the toolbar (the little green bug with the drop-down arrow) a. |.

2. Pull the drop-down menu and choose the Debug Configuration you created. (If you do not see it listed, choose the Debug Configurations... item and select the appropriate configuration. The Debug Configuration shows up on this drop-down list the next time you run the configuration.)

3. Continue as you did with the Run Configuration and choose the appropriate AVD and launch the emulator again, unlocking it if needed.

It takes a moment for the emulator to start up and for the debugger to attach. If this is the first time you've debugged an Android application, you need to click through some dialog boxes, such as the one shown in Figure 3.13, the first time your application attaches to the debugger.

Figure 3.12 My First Android App crashing gracefully.
Figure 3.13 Switching debug perspectives for Android emulator debugging.

In Eclipse, use the Debug perspective to set breakpoints, step through code, and watch the LogCat logging information about your application. This time, when the application fails, you can determine the cause using the debugger.You might need to click through several dialogs as you set up to debug within Eclipse. If you allow the application to continue after throwing the exception, you can examine the results in the Debug perspective of Eclipse. If you examine the LogCat logging pane, you see that your application was forced to exit due to an unhandled exception (see Figure 3.14).

Figure 3.14 Debugging My First Android App in Eclipse.

Specifically, there's a red AndroidRuntime error: java.lang.Error: Whoops. Back in the emulator, click the Force Close button. Now set a breakpoint on the forceError() method by right-clicking on the left side of the line of code and choosing Toggle Breakpoint (or Ctrl+Shift+B).

In Eclipse, you can step through code using Step Into (F5), Step Over (F6), Step Return (F7), and Resume (F8).

On Mac OS X, you might find that the F8 key is mapped globally. If you want to use the keyboard convenience command, you might want to change the keyboard mapping in Eclipse by choosing Eclipse, Preferences, General, Keys and finding the entry for Resume and changing it to something else. Alternatively, you can change the Mac OS X global mapping by going to System Preferences, Keyboard & Mouse, Keyboard Shortcuts and then changing the mapping for F8 to something else.

In the emulator, restart your application and step through your code.You see MyFirstAndroidApp has thrown the exception and then the exception shows up in the Variable Browser pane of the Debug Perspective. Expanding the variables contents shows that it is the "Whoops" error.

This is a great time to crash your application repeatedly and get used to the controls. While you're at it, switch over to the DDMS perspective.You note the emulator has a list of processes running on the phone, such as system_process and If you launch MyFirstAndroidApp, you see com.androidbook.myfirstandroidapp show up as a process on the emulator listing. Force the app to close because it crashes, and you note that it disappears from the process list.You can use DDMS to kill processes, inspect threads and the heap, and access the phone file system.

Was this article helpful?

0 0
Easy Software Profits

Easy Software Profits

Here's How Anyone Can Start, Build and Run their Own Successful Software Empire Without Knowing the First Thing About Programming, Scripting Or Any 'Coder' Stuff. There is no doubt about it software sells. Software has a high percieved value and it solves problems for people.  A claim that can't always be made for PDF books, Video products and the like.

Get My Free Ebook

Post a comment