Está en la página 1de 7

Debugging with Eclipse

Figure 2.7
Error markers in the Eclipse editor.

The error, indicated by a red circle, indicates a missing semicolon at the end of the
line, a punctuation error, so to speak. If you spot one of these and you dont immediately know what the problem is, hold the mouse pointer over the red circle (or
square, in the case of certain errors) a moment, and an explanation of the error
appears.
The last type of error, the run-time error, can be the most frustrating. That is because
this error occurs when the application is running, and there is no indication at compile time that anything is wrong. A run-time error turns up in the emulator when the
application is running and generally looks like Figure 2.8.

Figure 2.8
A run-time error indication in the emulator.

Whats more, due to the nature of run-time errors, they dont occur every time the
application runs. Unlike syntax errors, when run-time errors occur, the emulator
gives no explanation. Frustrating, indeed! This is where the Debug perspective in
Eclipse is the biggest help.

21

22

Chapter 2 n Starting an Android Application Project

The error in Figure 2.8 is the result of the following application code:
package com.sheusi.Debugging;
import android.app.Activity;
import android.os.Bundle;
public class DebuggingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int bogus[]=new int[5];
for(int ct=0;ct<=5;++ct){
bogus[ct]=ct;
}
}
}

Close examination reveals that we are outrunning the array of integers, which causes
an unchecked Java exception: ArrayIndexOutOfBounds Exception. Java programmers are familiar with exception handling. To look at the Debug perspective in
Eclipse, choose View Perspective under the Window menu, and choose Debug. You
will see a screen similar to Figure 2.9.

Figure 2.9
Eclipse Debug perspective.

Debugging with Eclipse

In the lower-right corner is a panel with a tab marked LogCat. This is the first place
to check for the source of the error. Any text in red is what you should examine.
Look at the close-up in Figure 2.10.

Figure 2.10
Eclipse LogCat panel.

Notice the line Caused by: java.lang.ArrayIndexOutOfBoundsException.


Here is our problem. Now we know what to look for in our code to correct the problem. Lets go back and fix the problem with a try-catch block. In the catch portion,
we will print some diagnostics. Notice that we cannot use the typical printStackTrace( ) on a mobile application to help us. Instead, we can use the logger. Notice
the changes in the following code:
package com.sheusi.Debugging;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class DebuggingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int bogus[]=new int[5];
try{
for(int ct=0;ct<=5;++ct){
bogus[ct]=ct;
}
}catch(ArrayIndexOutOfBoundsException aioe){
String err;
if(aioe.getMessage()==null)

23

24

Chapter 2 n Starting an Android Application Project


err=Outran Array;
else
err=aioe.getMessage();
Log.i(Error Here,err);
}
}
}

Notice that we added a try-catch block, a Log statement, and an additional import
statement to include the android Log class on our namespace. The try-catch block
allows the application to run, but if we take another look at the debug screen, we
find the LogCat section shown in Figure 2.11.

Figure 2.11
Eclipse LogCat panel showing user Log messages.

Under Tag, we see Error Here, which was the tag specified as the first argument in
the Log.i ( ) method call and the message we specified. If the exception had a message to print, it would have appeared in place of Outran Array. We can use the
Log class methods in place of the traditional printStackTrace( ). The I to the
left of the Pid column indicates that this is an INFO, or informational message.
This is specified by the use of the .i( ) method in the Log class. Other common
choices are D for DEBUG, E for ERROR, and W for WARNING.
We can also use the Eclipse debugger to check the values of variables at run-time by
setting breakpoints in our code. In fact, the use of breakpoints to check values is useful during application design and testing and need not involve errors. For instance,
you may want to check intermittent values during execution of a loop, or values of
variables during calculation of a complex formula. To set a breakpoint, just doubleclick on the line number (or the left margin if you are not using line numbers) next
to the line you would like execution to pause at. Next, switch to the debug perspective, and start the application by using the green bug icon instead of the green circle
icon at the top of the screen.

Debugging with Eclipse

When execution reaches the line where you set the breakpoint, that line becomes
highlighted in the code screen, and the variables and corresponding values appear
one by one under the Variables tab on the upper right. In the example, the breakpoint was set at line 87. See Figure 2.12.

Figure 2.12
Eclipse debug screen showing a breakpoint.

Keep in mind that Android applications are event driven, so you may need to bring
up the emulator and perform the necessary actions on it for the debugger to reach
the breakpoint, such as clicking a button onscreen. If you want execution to continue
beyond the breakpoint, you can use the following function keys to obtain the corresponding results.
F5

Execution resumes at the next step. If the next step is a method call, the
debugger jumps to that method.

F6

Execution runs the method, but the debugger does not step through the
method.

F7

Execution runs the method, but the debugger steps through it.

F8

Execution continues until the next breakpoint is encountered.

You can find more comprehensive guides to the use of the Eclipse debugger on the
Internet. One example in particular is a series of videos and a companion PDF called

25

26

Chapter 2 n Starting an Android Application Project

Eclipse and Java: Using the Debugger Companion Tutorial Document by Mark
Dexter. You can find them at http://eclipsetutorial.sourceforge.net.

Follow-Up
1. Research and practice constructing XML files. Learn the key components.
2. Fill in the following chart of the versions of the Android OS and the corresponding API levels. Research the improvements and key features for each release.
3. Study and practice the Eclipse Debugger.

Android Platform Number

API Level

Added Features and Improvements

Key Classes Used in This Chapter


Activity
Class

Activity

Package
Extends

android.app
Android.view.ContextThemeWrapper

Key Classes Used in This Chapter

An activity is generally a single-purpose screen and user interface. The activity takes
care of creating the window on which the application designer places controls that
allow the user to interact with the activity.
Figure 2.13 shows detail of a running activitys different states and the methods that
respond to the states. In this chapter we find the onCreate( ) method, which

Figure 2.13
Simple Activity class state chart.

27

También podría gustarte