Domanda

I'm trying to get the AndroidRuntime Exception StackTrace same as Logcat. However, when I Override the uncaughtException of UncaughtExceptionHandler, it gives me a different StackTrace other than the LogCat one. Here is my code:

private Thread.UncaughtExceptionHandler aovUncaughtExceptionHandler = 
    new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            StackTraceElement stackTrace[] = thread.getStackTrace();
            int stackTraceLength=stackTrace.length;
            String exceptionDescription = "";
            for (int i=0; i<stackTraceLength; i++) {
                exceptionDescription = exceptionDescription + stackTrace[i];
            }
            setException(exceptionDescription,true);
            applicationExceptionHandler.uncaughtException(thread,ex);
        }
    };

public AovGaTracker() {
    applicationExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(aovUncaughtExceptionHandler);
}

Here is the Stacktrace when using Exception as a stacktrace source:

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
android.app.ActivityThread.access$800(ActivityThread.java:139)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
android.os.Handler.dispatchMessage(Handler.java:102)
android.os.Looper.loop(Looper.java:136)
android.app.ActivityThread.main(ActivityThread.java:5102)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
dalvik.system.NativeStart.main(Native Method)

Here is the stackTrace when using Thread as a stackTace source:

dalvik.system.VMStack.getThreadStackTrace(Native Method)
java.lang.Thread.getStackTrace(Thread.java:579)
com.example.googleanalytics.AovGaTracker$1.uncaughtException(AovGaTracker.java:28)
java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
dalvik.system.NativeStart.main(Native Method)

Here is the supposed to be StackTrace from Logcat (which is what I need):

FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 18207
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}:
java.lang.IllegalArgumentException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException
at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
... 11 more

Is there anyway that I can execute getting the stacktrace after uncaughtException(Thread thread, Throwable ex) is executed? Do I need to use asynctask? Do I need to program asynctask on the same thread?

È stato utile?

Soluzione

There is always only one uncaught exception depending on the location of the exception set during runtime.

The exception in log cat is a combination of the stack trace, and the cause of the exception. You can combine exception.getStackTrace() and exception.getCause() to get the same result as logcat.

Altri suggerimenti

In a perfect world DefaultExceptionHandler works like a charm for me throughout application. Where it causes unhandled Exception it triggers up.

Library named Android-Remote-StackTrace. You just need to register the broadcast of these DefaultHandlerException class in your Application class.

Snippet:

 public class Portfolio extends Application {
 static Portfolio portfolio;
 public void onCreate() {
    super.onCreate();
    portfolio = this;
    ExceptionHandler.register(portfolio);// registering for unhandled Exceptions
   }
 }

You can do various task with these such as uploading the log stack trace to your server or send mails on the crash of application as a Crash Report with your unhandled Stack Trace.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top