我对stackoverflow很新。
我在我的android应用程序中实现了一个owne uncaughtException处理程序。 问题是当我的一个活动中发生一个unhandeld异常时,方法“uncaughtException”会被调用多次。
这是我的全班负责处理未被捕获的重复:
public class CustomUncaughtExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler {
private UncaughtExceptionHandler defaultUEH;
public CustomUncaughtExceptionHandler(UncaughtExceptionHandler defaultHandler) {
defaultUEH = defaultHandler;
Log.w("cmhandler","setted default UEH");
}
public void uncaughtException(Thread thread, Throwable exception) {
Log.w("cmhandler","uncaughtException");
Helper.Log_e("CustomUncaughtExceptionHandler", "uncaughtException", exception);
defaultUEH.uncaughtException(thread, exception);
}
}
行Helper.Log_e("CustomUncaughtExceptionHandler", "uncaughtException", exception);
仅将异常保存在文件中,并且不会抛出任何异常。
我在我的活动中实现了这样的课程:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new CustomUncaughtExceptionHandler(Thread.getDefaultUncaughtExceptionHandler()));
当我运行我的应用程序并在活动的oncreate中插入类似的东西时(在上面的代码之后)
String i = null;
i.length();
异常正确获取handeld并且弹出FC对话框。到目前为止一切都那么好,但在查看我的日志之后,我看到方法uncaughtException被多次调用。
编辑:通常该方法被调用2-6次,而来自defaultUEH设置的日志只在日志中出现一次。
还有其他人有过这样的行为吗?或者有人暗示我的错误吗?
谢谢你, 最好的问候schw4ndi
答案 0 :(得分:0)
我的错误是我在onCreate中设置了evrey活动中的默认Exceptino处理程序,因此通过调用getDefaultExceptionHandler,我得到了preios setted customExceptionHandler。
我现在通过将课程变成singelton来解决它。所以默认处理程序只设置一次。