//捕获异常类
import android.util.Log;
public class ExecptionHandler implements Thread.UncaughtExceptionHandler {
private static ExecptionHandlerexecptionHandler = null;
private ExecptionHandler() {
}
//这个方法是要在application里面调用的,分为一级一级,一级一级调
public static ExecptionHandlergetInstance(){
if (execptionHandler ==null){
synchronized(ExecptionHandler.class){
if (execptionHandler== null){
execptionHandler= new ExecptionHandler();
}
}
}
return execptionHandler;
}
//在application调用,第三级
public voidsetDefaultUnCachExceptionHandler(){
//设置应用默认的全局异常捕获器
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public voiduncaughtException(Thread thread, Throwable throwable) {
Log.d("myTag","程序出现异常"+throwable);
}
}
Application类
import android.app.Application;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ExecptionHandler.getInstance().setDefaultUnCachExceptionHandler();
}
}