自定义一个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">
转载请注明原文地址: https://ju.6miu.com/read-1295659.html