Application中捕获APP中的全局异常
1 package com.example.administrator.mystudent; 2 3 import android.app.Application; 4 import android.util.Log; 5 6 import org.xutils.x; 7 8 /** 9 * Created by hyang on 2016/8/23. 10 */ 11 public class MyApplication extends Application { 12 private static final String TAG ="Global" ; 13 14 @Override 15 public void onCreate() { 16 super.onCreate(); 17 //初始化xUtils 18 x.Ext.init(this); 19 //输出调试信息 20 x.Ext.setDebug(true); 21 getAnyException(); 22 } 23 24 /** 25 * 捕获全局的任意异常 26 */ 27 private void getAnyException() { 28 Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { 29 @Override 30 public void uncaughtException(Thread thread, Throwable ex) { 31 ex.printStackTrace(); 32 Log.e(TAG,"捕获到异常"+ex.toString()); 33 //1.把捕获到的异常存储到本地文件中 34 //2.处理把异常文件上传到服务器 35 //3.正常退出App(避免出现弹出对话框强制退出) 36 System.exit(0); 37 } 38 }); 39 } 40 }