- 自定义一个application
- 然后再清单文件中的 application 标签下添加name = MyApplication
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Thread.currentThread().setUncaughtExceptionHandler(new MyUncaughtException());
}
private class MyUncaughtException implements Thread.UncaughtExceptionHandler{
@Override
public void uncaughtException(Thread thread, Throwable ex) {
try {
ex.printStackTrace(new PrintStream(new File(getFilesDir() , "log.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Process.killProcess(Process.myPid());
}
}
}
<application
android:name=".MyApplication"//这个一定要添加!!!!
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">