質問

i'am relativ new to stackoverflow.

I have implemented a owne uncaughtException handler in my android application. The problem is that the method "uncaughtException" gets called muliple times when one unhandeld exception occurs in one of my activitys.

Here is my whole class responsible for the handling of an uncaught excpetion:

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);
}

}

The line Helper.Log_e("CustomUncaughtExceptionHandler", "uncaughtException", exception); only saves the exception in a file, and there where no exceptions thrown.

I implemented the class in my activity like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Thread.setDefaultUncaughtExceptionHandler(new CustomUncaughtExceptionHandler(Thread.getDefaultUncaughtExceptionHandler()));

When i run my app and insert something like that in the oncreate of the activity (after the code above)

String i = null;
    i.length();

The exception gets handeld correctly and also the FC dialog pops-up. So far so good, but after looking into my logs i see that the method uncaughtException gets called multiple times.

EDIT: Usually the method gets called 2-6 times, and the log from the settings of the defaultUEH only appears once in the logs.

Did anyone else has such a behavior yet? Or does anyone have a hint for my what i'am doing wrong?

Thank you, best regards schw4ndi

役に立ちましたか?

解決

My mistake was that i setted the default Exceptino Handler in evrey activity in the onCreate, so by calling getDefaultExceptionHandler i get the previos setted customExceptionHandler.

I now solved it by making the class to a singelton. So the default handler only gets setted once.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top