Introduction
Logcat is an important tool in Android studio. Logcat helps developers to debug the code when an application is not running or has crashed. Suppose, we are developing a large application such as a Soical Media application, E-Commerce application, News application etc. Such applications have lots of modules and contain n number lines of code. If our application crashes then it is very difficult to find out the errors that occured when our application was running. Logcat is nothing but a solution to this type of problem. Logcat helps developers to detect and solve the error which caused application crash.
How to find Logcat in Android Studio?
Step 1
Just go to bottom section of Android Studio and find out the logcat option and click on it.
Step 2
After Clicking Logcat option, a new window will be open. This is our logcat window.
Here, no device is connected yet so logcat is empty. Sometime logcat still empty even after application crashed. In this situation just press the restarted button which appears on the left side of logcat window.
How logcat works?
Let's take a simple example
Step 1
Create new project in android studio and select empty activity
Click on Finish Button.
Step 2
Now go to activity_main.xml. By default we get a textview so just set the id of this textview.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3
Now go to MainActivity.java. Create a Textview object and same time initialize with the help of findViewById() method.
TextView textview=findViewById(R.id.textview);
MainActivty.java
package com.example.logcatexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView=findViewById(R.id.textview);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Step 4
Now connect any physical or virtual device to android studio and click on Run button. The application is installed however immediately crashed.
The solution
We can solve this problem using logcat.
Step 1
Open Logcat and again click on run button. In logcat some line of code is generated so we have to find out the line that tells us which line of code contains error. Sometimes we need to scroll up in locat window to find out the error. Examine the below picture carefully and an orange color arrow indicate an error line.

Join the conversation! Your thoughts help the community grow.