Android开发之日志记录(六)

日志级别:error > warn > information > debug > verbose 

 日志类型

                //Log.e():error级别
                Log.e("LogActivity","我是错误日志");

                //Log.w():warn级别
                Log.w("LogActivity","我是警告日志");

                //Log.i():information级别
                Log.i("LogActivity","我是信息日志");

                //Log.d():debug级别
                Log.d("LogActivity","我是调试日志");

                //Log.v():verbose级别
                Log.v("LogActivity","我是冗余日志");

查看日志:控制台下导航栏Logcat

 [例子]

log_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/l_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名" />

    <EditText
        android:id="@+id/l_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码" />

    <Button
        android:id="@+id/l_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录" />
</LinearLayout>

LogActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LogActivity extends AppCompatActivity {

    //定义变量
    EditText lUser;
    EditText lPwd;
    Button lBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);

        //初始化变量
        lUser=findViewById(R.id.l_user);
        lPwd=findViewById(R.id.l_pwd);
        lBtn=findViewById(R.id.l_btn);

        //监听按钮点击事件
        lBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取信息
                String user =lUser.getText().toString().trim();
                String passwd =lPwd.getText().toString().trim();

                try {
                    int a=2/0;
                }catch (Exception e){
                    //Log.e():error级别
                    Log.e("LogActivity",e.toString());
                }
            }
        });
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值