Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Getting Started  ·  Lesson 08

Logcat, Debugging & Emulator

Logging with Log.d and friends

println() does not help much on a phone. Use the Log class — messages appear in the Logcat window with time, tag and level.

Kotlin
import android.util.Log

private const val TAG = "NotesScreen"

Log.v(TAG, "verbose – very detailed")
Log.d(TAG, "debug – notes loaded: ${notes.size}")
Log.i(TAG, "info – user opened editor")
Log.w(TAG, "warning – reminder time is in the past")
Log.e(TAG, "error – could not save note", exception)   // prints the stack trace too
LevelFunctionUse for
VerboseLog.vEverything (rarely needed)
DebugLog.dValues while developing
InfoLog.iImportant normal events
WarnLog.wSomething unusual but handled
ErrorLog.eFailures, with the exception

Filtering Logcat

Logcat filter box
package:mine                 # only your app
tag:NotesScreen              # only one tag
level:error                  # errors and above
package:mine level:warn      # combine filters
"notes loaded"               # free text search

Reading a crash (stack trace)

When the app closes with "App keeps stopping", Logcat shows a red FATAL EXCEPTION. Read it from the top and find the first line with your package.

Kotlin – buggy code
class MainActivity : AppCompatActivity() {
    private lateinit var tvTitle: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        tvTitle.text = "Hello"                      // line 18 – used before it was set!
        setContentView(R.layout.activity_main)
    }
}
Logcat – crash
FATAL EXCEPTION: main Process: com.example.helloapp, PID: 12345 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloapp/com.example.helloapp.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property tvTitle has not been initialized at com.example.helloapp.MainActivity.onCreate(MainActivity.kt:18) ← YOUR file and line at android.app.Activity.performCreate(Activity.java:8595) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3790) ...
  1. 1Find "FATAL EXCEPTION" in red
  2. 2Read the exception type and message (what went wrong)
  3. 3Find the first "at com.your.package…(File.kt:line)" (where)
  4. 4Click the blue file link → Android Studio jumps to the line
  5. 5Fix, run again
Every crash tells you what and where
ExceptionUsual cause
NullPointerException / UninitializedPropertyAccessExceptionUsing a view or variable before it is set
ActivityNotFoundExceptionActivity not in AndroidManifest.xml
NetworkOnMainThreadExceptionInternet call on the main thread
Resources.NotFoundExceptionPassing a number where a string resource ID was expected (setText(5))
IllegalStateException: Cannot access database on the main threadRoom query without a coroutine

Breakpoints and the debugger

  1. 1Click in the gutter left of a line number → red dot (breakpoint)
  2. 2Press the bug icon 🐞 (Debug) instead of ▶ Run
  3. 3App pauses at the line — see all variable values in the Variables panel
  4. 4Step Over (F8) runs the next line · Step Into (F7) enters a function
  5. 5Resume (F9) continues until the next breakpoint
Pause the app and look inside
🔒 Android Studio – Debug● 18 tvTitle.text = "Note: $title" ← paused herethis = MainActivity@8f2ctitle = "Maths homework"count = 3F8 step over · F7 step into · F9 resume

Layout Inspector

Tools → Layout Inspector shows the live view tree of the running app in 3D. Use it when a view is invisible, has size 0 or is hidden behind another view.

Emulator power tips

  • The ⋯ Extended controls panel lets you set fake GPS location, battery level and network speed.
  • Rotate with the rotate buttons to test state saving.
  • Drag an image file onto the emulator to put it in the gallery (for photo-picker testing).
  • Cold boot from Device Manager if the emulator freezes.

A debugging routine

Reproducesame stepsLogcaterror + lineBreakpointcheck valuesFixone changeRe-testall steps
Change one thing at a time

Common mistakes

  • Reading only the last line of a stack trace → the cause is near the top (and in "Caused by:").
  • Logging private data (passwords, tokens).
  • Debugging the release build → breakpoints do not work; use the debug build.

Summary

  • Log.d/i/w/e(TAG, msg) + Logcat filters (package:mine, tag:, level:).
  • Crash = FATAL EXCEPTION → type + first line in your package.
  • Breakpoints + Debug ▶ to see values; Layout Inspector for UI problems.
✅ Practice

Create the crash above on purpose, read it in Logcat, click the link to jump to the line and fix it. Then set a breakpoint in a click listener and inspect a variable.

Frequently Asked Questions

Why is my Logcat empty?
Check that the correct device and app process are selected at the top of Logcat and that your filter (for example package:mine) is not hiding the messages.
Should I remove Log statements before release?
Never log passwords or personal data. Debug logs are harmless but noisy; many apps remove them in release builds with R8 rules or wrap them in if (BuildConfig.DEBUG).
What is an ANR?
Application Not Responding: the main thread was blocked for about 5 seconds, usually by network or database work. Move that work to a coroutine on a background dispatcher.
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicLinearLayout, FrameLayout & RelativeLayout Your progress: 8 / 43 (19%) · Lesson 9 / 43 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free