Logcat 是一个命令行工具,用于转储系统消息日志,包括设备抛出错误时的堆栈轨迹,以及从您的应用中使用 Log 类写入的消息。
通过 Log 类,您可以在代码中创建日志条目,而这些条目会显示在 Logcat 工具中。常用的日志记录方法包括:
Log.v(String, String)(详细)
Log.d(String, String)(调试)
Log.i(String, String)(信息)
Log.w(String, String)(警告)
Log.e(String, String)(错误)
package com.example.helloworld;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private String TAG="LOGLOG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG,"onCreate");
}
@Override
protected void onStart()
{
super.onStart();
Log.i(TAG,"onStart");
}
@Override
protected void onResume()
{
super.onResume();
Log.i(TAG,"onResume");
}
@Override
protected void onPause()
{
super.onPause();
Log.i(TAG,"onPause");
}
@Override
protected void onStop()
{
super.onStop();
Log.i(TAG,"onStop");
}
@Override
protected void onRestart()
{
super.onRestart();
Log.i(TAG,"onRestart");
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.i(TAG,"onDestroy");
}
@Override
protected void onSaveInstanceState(Bundle save)
{
super.onSaveInstanceState(save);
Log.i(TAG,"onSaveInstanceState");
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle SaveInstanceState)
{
super.onRestoreInstanceState(SaveInstanceState );
Log.i(TAG,"onRestoreInstanceState");
}
}
<?xml version="1.0" encoding="utf-8"?>
: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">
:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="实验1"
android:textSize="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
>