Tutorial 7

Debug without guessing.

Beginners lose hours because they change random code. Debugging is a repeatable loop: reproduce, read the error, isolate the cause, fix one thing, verify.

Error types

Compiler error: app cannot build because code is invalid.
Runtime crash: app builds but fails while running.
Logic bug: app runs but behavior is wrong.
UI bug: app works but layout, text, or interaction is wrong.

Add useful logs

private const val TAG = "HabitTracker"

fun addHabit(title: String) {
    Log.d(TAG, "Adding habit: $title")

    if (title.isBlank()) {
        Log.w(TAG, "Ignored blank habit")
        return
    }

    // Add habit to state here
}

Logs should explain what the app is doing. Avoid dumping private user data into logs.

Practice task

  • Intentionally remove a closing parenthesis and read the compiler error.
  • Add a log when the Add button is clicked.
  • Use a breakpoint inside a click handler.
  • Create a debugging note: symptom, cause, fix, verification.

Reference: Android Studio debugging documentation covers breakpoints, the debugger, and inspecting app behavior during execution.