-
Title:
05-24 Debug Mode in Android Studio
-
Description:
05-24 Debug Mode in Android Studio
-
We're making really good progress so
far.
-
As we continue to build
more challenging things for
-
our app, we're going to have to
improve our debugging skills.
-
In this video,
-
we're going to learn about how to
use a tool called the debugger.
-
We'll access the debugger
using Android Studio, and
-
the debugger is meant to help us
identify and fix errors in our code.
-
The great thing about the debugger is
that we can pause the app at a certain
-
point in time, and then we can inspect
the whole state of the app more closely.
-
Normally, when the app is
running on the device,
-
all the code gets executed very rapidly.
-
Within milliseconds, it can respond
to button clicks, update the screen,
-
calculate the price, and etcetera.
-
But with the debugger, we can pause at
a specific line of code in our app,
-
then we can step through our code line
by line as quickly or slowly as we want.
-
If you want to learn more techniques on
how to debug your app in Android Studio,
-
just search for
debugging Android Studio.
-
This first link is good,
it's a developer.android.com page and
-
it has a bunch of techniques
on how to debug your app.
-
Together we're going to learn about
how to work with breakpoints.
-
After this course,
-
you can always come back to this
page to refresh your memory.
-
In this exercise, I want to show you
that your Android device does run
-
each line of code one at at time,
from top to bottom, within a method.
-
We'll also verify that clicking
on the + button does trigger
-
the increment method.
-
And clicking on the- button does
trigger the decrement method.
-
First add something
known as a breakpoint.
-
A breakpoint marks
a specific line of code
-
where the debugger should
pause when it reaches here.
-
The red circular dot indicates
that the breakpoint is
-
on this line on line 25 of
our main activity file.
-
You can add breakpoints to as many
places as you want in your code.
-
But for now,
-
I'm just going to add one on the first
line of the increment method.
-
Then I can hit this bug icon
to run the app in debug mode.
-
The app will only pause
on these breakpoints,
-
when the app is running in debug mode.
-
If you run the app normally,
-
with the screen play button, then it
won't pause at these break points.
-
For a brief moment on the device you
may have seen a dialogue that says,
-
waiting for debugger.
-
There's also a button to force close
the app, but don't click on that.
-
Just wait for the debugger to attach,
and the message will go away soon.
-
Then the app starts like normal and
you can interact with it.
-
Now in debug mode, this window will pop
up and show the status of the debugger.
-
If you don't see it, you can click
on this tab down here called, Debug.
-
Now if I hit this + button here,
I expect the increment method will be
-
triggered and that the app
will pause at this breakpoint.
-
Let's see what happens.
-
Cool, it stops there.
-
In the Debug pane,
we see a list of variables.
-
This reflects the current state
of the app where it was paused.
-
We don't see our quantity variable yet
-
because it hasn't finished
executing this line of code yet.
-
There's a bunch of options in how
to proceed with debugging, but
-
I'm going to click on this
option that says step over.
-
This will step over the current line
of code to the next line of code.
-
Now line 25 hasn't been executed, so
-
there's a quantity variable showing
up in the Variables pane here.
-
Now the app is currently
paused at line 26.
-
We finished executing line 25,
-
so that's why we see the quantity
variable showing up down here.
-
The value of the variable is set to 3,
which is expected.
-
This list of variables also shows
the current value of each variable.
-
We can also verify that the quantity
variable has a value of 3,
-
which we assigned up here.
-
We know that the app is currently paused
at this point, because normally when you
-
hit the + button, it would automatically
update the quantity to be 3.
-
But since the quantity is still at 2,
-
we know that it hasn't finished
executing the increment method yet.
-
We can click step over
to go to the next line.
-
At this point the method is done,
-
and we're not really interested in
how it actually updates this screen.
-
So let's just hit resume program.
-
All we care about is that it
actually does update the screen.
-
If you're interested, you could have
clicked on these other options to look
-
at more details of how it
actually updates the screen.
-
At this point the app
is running normally.
-
But the debugger is still attached.
-
So if I hit the + button again,
it will stop at this breakpoint.
-
If I want to detach the debugger so that
it doesn't always stop on this line when
-
I hit the + button, I can just
click on this red stop button.
-
Now, whenever I hit the + button
it responds immediately and
-
updates the value to 3.
-
Now I want you to try
it on your computer.
-
Go ahead and add a breakpoint to
the increment and decrement methods.
-
Then run the app in Debug Mode,
-
and then on the device, try to
trigger each of these breakpoints.
-
Then step through each line of code,
making sure that the quantity
-
variable shows up with the right
value in the variables list.
-
You can click on this play button
to resume execution of the app so
-
that it runs as normal.
-
Or you can hit the stop button
to detach the debugger.