Android Fundamentals 03.1 : The Debugger
By: Kevin Reynaldi
Tags: Debugging

Helloo, my friends…

Welcome back to my tutorial ,in this week i will make a tutorial with the explanation to all of you about Debugger Application. This medium will help you learn more deep about android code. And because of this i hope you enjoy my tutorial. Thank You.

3. Task 1: Explore the SimpleCalc project and app

In this practical you won’t build the SimpleCalc app by yourself. But you must download the Complete Project at SimpleCalc. After you download this task you open it the SimpleCalc on project in Android Studio.

Download and Open the SimpleCalc Project

A. Download SimpleCalc on this link.

B. Start Android Studio and Select File>Open.

C. This The Result

2. Explore the Layout

A. Open activity_main.xml.

B. Click the Text tab to see the XML code.

C. Click the Preview tab to see a preview of the layout. And Then make sure XML and Design Tab are according this Picture below.

3. Explore the app code

A. Expand the app > java folder in the Project > Android pane. In addition to the MainActivity class, this project also includes a utility Calculator class.

B. Expand the app > java folder in the Project > Android pane. In addition to the MainActivity class, this project also includes a utility Calculator class.

C. Open MainActivity, and examine the code and comments. And see if that code match the criteria on the Second Picture.

4. Run the App

Enter both integer and floating-point values for the calculation.

2. Enter floating-point values with large decimal fractions (for example, 1.6753456)

3. Divide a number by zero.

4. Leave one or both of the EditText elements empty, and try any calculation.

5. Click the Logcat tab at the bottom of the Android Studio window to open the Logcat pane (if it is not already open). Examine the stack trace at the point where the app reports an error.

Task 2: Run SimpleCalc in the debugger

In this task you’ll get an introduction to the debugger in Android Studio, and learn how to set a breakpoint and run your app in debug mode. Hope you can learn to do it.

Start and run your app in debug mode

A. In Android Studio, select Run > Debug app or click the Debug icon in the toolbar.

B. If your app is already running, you will be asked if you want to restart your app in debug mode. Click Restart app.

C. Click the Debug tab at the bottom of the Android Studio window to show the Debug pane (or choose View > Tool Windows > Debug). The Debugger tab in the pane should already be selected, showing the Debugger pane.

2. Set a breakpoint

A. Open MainActivity, and click in the fourth line of the compute() method (the line just after the try statement).

B. Click in the left gutter of the editor pane at that line, next to the line numbers. A red dot appears at that line, indicating a breakpoint. The red dot includes a check mark if the app is already running in debug mode.

C. In the SimpleCalc app, enter numbers in the EditText elements and click one of the calculate Button elements.

Frames Tab

Watches

Variables Pane

3. Resume your app’s execution

Resume your app’s execution by choosing Run > Resume Program, or click the Resume icon on the left side of the debugger window.

4. Debug a running app

A. Run the SimpleCalc app normally, with the Run icon.

B. Select Run > Attach debugger to Android process or click the Attach icon in the toolbar.

Task 3: Explore debugger features

In this task we’ll explore the various features in the Android Studio debugger, including executing your app line by line, working with breakpoints, and examining variables.

Step through your app’s execution

A. Debug your app in Android Studio, with the breakpoint you set in the last task.

B. In the app, enter numbers in both EditText elements, and click the Add button.

C. Click the Step Over button at the top of the debugger window.

D. At the next line (the assignment for operandTwo ), click the Step Into icon.

E. Click Step Over to run each of the lines in getOperand() . Note that when the method completes the debugger returns you to the point where you first stepped into the method, and all the panels update to show the new information.

F. Click Step Over twice to move the execution point to the first line inside the case statement for ADD .

G. Click Step Into

H. Use the Step Out icon to execute the remainder of that calculation method and pop back out to the compute() method in MainActivity . You can then continue debugging the compute() method from where you left off.

2. Work with Breakpoints

A. Find the breakpoint you set in the last task — at the start of the compute() method in MainActivity .

B. Add a breakpoint to the start of the switch statement.

C. Right-click on that new breakpoint to enter a condition, as shown in the figure below, and enter the following test in the Condition field:

D. Click Done

E. Run your app in debug mode (Run > Debug), or click Resume if it is already running. In the app, enter two numbers other than 42 and click the Add button. Execution halts at the first breakpoint in the compute() method.

F. Click Resume to continue debugging the app. Observe that execution did not stop at your second breakpoint, because the condition was not met.

G. In the app, enter 42 in the first EditText and click any Button . Click Resume to resume execution after the first breakpoint. Observe that the second breakpoint at the switch statement—the conditional breakpoint—halts execution because the condition was met.

H. Right-click (or Control-click) the first breakpoint in compute() and uncheck Enabled. Click Done. Observe that the breakpoint icon now has a green dot with a red border.

I. Click View Breakpoints on the left edge of the debugger window. The Breakpoints window appears.

3. Examine and modify variables

A. Run the SimpleCalc app in debug mode if it is not already running.

B. In the app, enter two numbers, one of them 42, and click the Add button.

C. Observe in the Variables pane that the operandOne and operandTwo variables have the values you entered into the app.

D. The this variable is a MainActivity object. Click the expand icon to see a list of member variables of that object. Click the expand icon again to close the list.

E. Right-click (or Control-click) the operandOne variable in the Variables pane, and select Set Value.

F. Change the value of operandOne to 10 and press Return.

G. Change the value of operandTwo to 10 in the same way and press Return.

H. Observe that the result in the app is now based on the variable values you changed in the debugger; for example, since you clicked the Add Button in Step 2, the result in the app is now 20.

I. Click the Resume icon to continue running your app.

J. In the app, the original entries (including 42) are preserved in the EditText elements. (Their values were changed only in the debugger.) Click the Add button. Execution halts at the breakpoint again.

K. Click the Evaluate Expression icon, or select Run > Evaluate Expression. You can also right-click (or Control-click) any variable and choose Evaluate Expression.

L. The Evaluate Code Fragment window appears. Use it to explore the state of variables and objects in your app, including calling methods on those objects. You can enter any code into this window.

M. Type the statement mOperandOneEditText.getHint(); into the top field of the Evaluate Code Fragment window (as shown in the figure above), and click Evaluate.

N. The Result field shows the result of that expression. The hint for this EditText is the string "Type Operand 1" , as was originally defined in the XML for that EditText .

Click Close to close the Evaluate Code Fragment window.