Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 शुरुआत  ·  Lesson 08

Logcat, Debugging और Emulator

Log.d वगैरह से logging

Phone पर println() ज़्यादा काम नहीं आता। Log class use करें — messages Logcat window में time, tag और 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
LevelFunctionकिसके लिए
VerboseLog.vसब कुछ (कम ज़रूरत)
DebugLog.dDevelopment में values
InfoLog.iज़रूरी normal events
WarnLog.wकुछ अजीब पर handled
ErrorLog.eFailures, exception के साथ

Logcat filter करना

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

Crash पढ़ना (stack trace)

जब app "App keeps stopping" के साथ बंद हो, Logcat में लाल FATAL EXCEPTION दिखता है। ऊपर से पढ़ें और आपके package वाली पहली line ढूँढें।

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. 1लाल "FATAL EXCEPTION" ढूँढें
  2. 2Exception type और message पढ़ें (क्या गलत हुआ)
  3. 3पहली "at com.your.package…(File.kt:line)" ढूँढें (कहाँ)
  4. 4नीले file link पर click → Android Studio उसी line पर ले जाता है
  5. 5Fix करें, फिर चलाएँ
हर crash बताता है क्या और कहाँ
Exceptionआम कारण
NullPointerException / UninitializedPropertyAccessExceptionView या variable set होने से पहले use करना
ActivityNotFoundExceptionActivity AndroidManifest.xml में नहीं
NetworkOnMainThreadExceptionMain thread पर internet call
Resources.NotFoundExceptionString resource ID की जगह number देना (setText(5))
IllegalStateException: Cannot access database on the main threadCoroutine के बिना Room query

Breakpoints और debugger

  1. 1Line number के बाईं ओर gutter में click → लाल dot (breakpoint)
  2. 2▶ Run की जगह bug icon 🐞 (Debug) दबाएँ
  3. 3App उस line पर रुकती है — Variables panel में सारी values देखें
  4. 4Step Over (F8) अगली line चलाता है · Step Into (F7) function के अंदर जाता है
  5. 5Resume (F9) अगले breakpoint तक चलाता है
App रोककर अंदर देखें
🔒 Android Studio – Debug● 18 tvTitle.text = "Note: $title" ← यहाँ रुकाthis = MainActivity@8f2ctitle = "Maths homework"count = 3F8 step over · F7 step into · F9 resume

Layout Inspector

Tools → Layout Inspector चलती app का live view tree 3D में दिखाता है। जब कोई view दिख न रहा हो, size 0 हो या दूसरे view के पीछे छिपा हो तब use करें।

Emulator power tips

  • ⋯ Extended controls panel से fake GPS location, battery level और network speed set कर सकते हैं।
  • State saving test करने के लिए rotate buttons से rotate करें।
  • Image file को emulator पर drag करें ताकि gallery में आ जाए (photo-picker testing के लिए)।
  • Emulator अटक जाए तो Device Manager से Cold boot करें।

Debugging routine

Reproduceवही stepsLogcaterror + lineBreakpointvalues checkFixएक बदलावRe-testसारे steps
एक बार में एक चीज़ बदलें

आम गलतियाँ

  • Stack trace की सिर्फ़ आख़िरी line पढ़ना → कारण ऊपर के पास (और "Caused by:" में) होता है।
  • Private data (passwords, tokens) log करना।
  • Release build debug करना → breakpoints काम नहीं करते; debug build use करें।

सारांश

  • Log.d/i/w/e(TAG, msg) + Logcat filters (package:mine, tag:, level:)।
  • Crash = FATAL EXCEPTION → type + आपके package की पहली line।
  • Values देखने के लिए Breakpoints + Debug ▶; UI problems के लिए Layout Inspector।
✅ Practice

ऊपर वाला crash जानबूझकर बनाइए, Logcat में पढ़िए, link से line पर जाकर fix कीजिए। फिर click listener में breakpoint लगाकर कोई variable inspect कीजिए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

मेरा Logcat खाली क्यों है?
Logcat के ऊपर सही device और app process चुना है या नहीं, और आपका filter (जैसे package:mine) messages छिपा तो नहीं रहा, यह check करें।
क्या release से पहले Log statements हटाने चाहिए?
Passwords या personal data कभी log न करें। Debug logs नुकसानदेह नहीं पर शोर करते हैं; कई apps release builds में R8 rules से हटाती हैं या if (BuildConfig.DEBUG) में रखती हैं।
ANR क्या है?
Application Not Responding: main thread लगभग 5 seconds block रहा, आमतौर पर network या database काम से। उस काम को background dispatcher वाले coroutine में ले जाएँ।
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

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

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

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

अगला topicLinearLayout, FrameLayout और RelativeLayout आपकी प्रगति: 8 / 43 (19%) · Lesson 9 / 43 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें