public class ExceptionA implements Thread.UncaughtExceptionHandler {
private Context context;
private Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler;
private static ExceptionA instance = new ExceptionA();
private ExceptionA() {
}
public static ExceptionA getInstance() {
return instance;
}
@Override
public void uncaughtException(Thread t, Throwable e) {
if (defaultUncaughtExceptionHandler != null) {
defaultUncaughtExceptionHandler.uncaughtException(t,e);
} else {
Log.d("========", e.toString());
}
}
public void init(Context context) {
this.context = context.getApplicationContext();
defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
}
app:
ExceptionA.getInstance().init(this);
MainActivity:
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView = null;
textView.setText("");
}
});