全局异常捕获
程序员文章站
2022-04-21 17:56:24
...
新建一个App类
一定要在清单文件注册
package com.example.am;
import android.app.Application;
import android.widget.Toast;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
public class App extends Application implements Thread.UncaughtExceptionHandler{
@Override
public void onCreate() {
super.onCreate();
//进行异常捕获
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
//进行日志打印
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
throwable.printStackTrace(printWriter);
Toast.makeText(this,"凉了",Toast.LENGTH_LONG).show();
}
}
//activity文件
package com.example.am;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class TwoActivity extends AppCompatActivity implements View.OnClickListener {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
button = findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name = null;
boolean status = name.equals("周扒皮");
Log.e("dgsdg","status"+status);
}
}
上一篇: 崩溃bug日志总结1