🟢 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| Level | Function | Use for |
|---|---|---|
| Verbose | Log.v | Everything (rarely needed) |
| Debug | Log.d | Values while developing |
| Info | Log.i | Important normal events |
| Warn | Log.w | Something unusual but handled |
| Error | Log.e | Failures, 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)
...
- 1Find "FATAL EXCEPTION" in red
- 2Read the exception type and message (what went wrong)
- 3Find the first "at com.your.package…(File.kt:line)" (where)
- 4Click the blue file link → Android Studio jumps to the line
- 5Fix, run again
| Exception | Usual cause |
|---|---|
| NullPointerException / UninitializedPropertyAccessException | Using a view or variable before it is set |
| ActivityNotFoundException | Activity not in AndroidManifest.xml |
| NetworkOnMainThreadException | Internet call on the main thread |
| Resources.NotFoundException | Passing a number where a string resource ID was expected (setText(5)) |
| IllegalStateException: Cannot access database on the main thread | Room query without a coroutine |
Breakpoints and the debugger
- 1Click in the gutter left of a line number → red dot (breakpoint)
- 2Press the bug icon 🐞 (Debug) instead of ▶ Run
- 3App pauses at the line — see all variable values in the Variables panel
- 4Step Over (F8) runs the next line · Step Into (F7) enters a function
- 5Resume (F9) continues until the next breakpoint
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
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.
