English subtitles

← 05-24 Debug Mode in Android Studio

05-24 Debug Mode in Android Studio

Get Embed Code
13 Languages

Showing Revision 2 created 08/18/2015 by Udacity Robot.

  1. We're making really good progress so
  2. As we continue to build
    more challenging things for
  3. our app, we're going to have to
    improve our debugging skills.
  4. In this video,
  5. we're going to learn about how to
    use a tool called the debugger.
  6. We'll access the debugger
    using Android Studio, and
  7. the debugger is meant to help us
    identify and fix errors in our code.
  8. The great thing about the debugger is
    that we can pause the app at a certain
  9. point in time, and then we can inspect
    the whole state of the app more closely.
  10. Normally, when the app is
    running on the device,
  11. all the code gets executed very rapidly.
  12. Within milliseconds, it can respond
    to button clicks, update the screen,
  13. calculate the price, and etcetera.
  14. But with the debugger, we can pause at
    a specific line of code in our app,
  15. then we can step through our code line
    by line as quickly or slowly as we want.
  16. If you want to learn more techniques on
    how to debug your app in Android Studio,
  17. just search for
    debugging Android Studio.
  18. This first link is good,
    it's a developer.android.com page and
  19. it has a bunch of techniques
    on how to debug your app.
  20. Together we're going to learn about
    how to work with breakpoints.
  21. After this course,
  22. you can always come back to this
    page to refresh your memory.
  23. In this exercise, I want to show you
    that your Android device does run
  24. each line of code one at at time,
    from top to bottom, within a method.
  25. We'll also verify that clicking
    on the + button does trigger
  26. the increment method.
  27. And clicking on the- button does
    trigger the decrement method.
  28. First add something
    known as a breakpoint.
  29. A breakpoint marks
    a specific line of code
  30. where the debugger should
    pause when it reaches here.
  31. The red circular dot indicates
    that the breakpoint is
  32. on this line on line 25 of
    our main activity file.
  33. You can add breakpoints to as many
    places as you want in your code.
  34. But for now,
  35. I'm just going to add one on the first
    line of the increment method.
  36. Then I can hit this bug icon
    to run the app in debug mode.
  37. The app will only pause
    on these breakpoints,
  38. when the app is running in debug mode.
  39. If you run the app normally,
  40. with the screen play button, then it
    won't pause at these break points.
  41. For a brief moment on the device you
    may have seen a dialogue that says,
  42. waiting for debugger.
  43. There's also a button to force close
    the app, but don't click on that.
  44. Just wait for the debugger to attach,
    and the message will go away soon.
  45. Then the app starts like normal and
    you can interact with it.
  46. Now in debug mode, this window will pop
    up and show the status of the debugger.
  47. If you don't see it, you can click
    on this tab down here called, Debug.
  48. Now if I hit this + button here,
    I expect the increment method will be
  49. triggered and that the app
    will pause at this breakpoint.
  50. Let's see what happens.
  51. Cool, it stops there.
  52. In the Debug pane,
    we see a list of variables.
  53. This reflects the current state
    of the app where it was paused.
  54. We don't see our quantity variable yet
  55. because it hasn't finished
    executing this line of code yet.
  56. There's a bunch of options in how
    to proceed with debugging, but
  57. I'm going to click on this
    option that says step over.
  58. This will step over the current line
    of code to the next line of code.
  59. Now line 25 hasn't been executed, so
  60. there's a quantity variable showing
    up in the Variables pane here.
  61. Now the app is currently
    paused at line 26.
  62. We finished executing line 25,
  63. so that's why we see the quantity
    variable showing up down here.
  64. The value of the variable is set to 3,
    which is expected.
  65. This list of variables also shows
    the current value of each variable.
  66. We can also verify that the quantity
    variable has a value of 3,
  67. which we assigned up here.
  68. We know that the app is currently paused
    at this point, because normally when you
  69. hit the + button, it would automatically
    update the quantity to be 3.
  70. But since the quantity is still at 2,
  71. we know that it hasn't finished
    executing the increment method yet.
  72. We can click step over
    to go to the next line.
  73. At this point the method is done,
  74. and we're not really interested in
    how it actually updates this screen.
  75. So let's just hit resume program.
  76. All we care about is that it
    actually does update the screen.
  77. If you're interested, you could have
    clicked on these other options to look
  78. at more details of how it
    actually updates the screen.
  79. At this point the app
    is running normally.
  80. But the debugger is still attached.
  81. So if I hit the + button again,
    it will stop at this breakpoint.
  82. If I want to detach the debugger so that
    it doesn't always stop on this line when
  83. I hit the + button, I can just
    click on this red stop button.
  84. Now, whenever I hit the + button
    it responds immediately and
  85. updates the value to 3.
  86. Now I want you to try
    it on your computer.
  87. Go ahead and add a breakpoint to
    the increment and decrement methods.
  88. Then run the app in Debug Mode,
  89. and then on the device, try to
    trigger each of these breakpoints.
  90. Then step through each line of code,
    making sure that the quantity
  91. variable shows up with the right
    value in the variables list.
  92. You can click on this play button
    to resume execution of the app so
  93. that it runs as normal.
  94. Or you can hit the stop button
    to detach the debugger.