when I develop android application,I want to make a CrashReport class and then use it send report to my server.
I make a class called CrashHandler which implement UncaughtExceptionHandler,and make a example to cause a NullPoint error in Activity in purpose,the class works well , but a little problem... in method uncaughtException:
@Override
public void uncaughtException(Thread thread, Throwable ex) {
ex.printStackTrace(); //**code line 1**
....//some other codes and variables
StackTraceElement[] elements = ex.getStackTrace();
for (StackTraceElement element : elements) { // **code line 2**
errorString = errorString + "<br/>" + element.toString();
}
}
code line 1 print the stack trace as:
java.lang.NullPointerException
my.app.ExceptionCaughtActivity$1.onClick(ExceptionCaughtActivity.java:25)
android.view.View.performClick(View.java:2486)
android.view.View$PerformClick.run(View.java:9130)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:130)
android.app.ActivityThread.main(ActivityThread.java:3703)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
dalvik.system.NativeStart.main(Native Method)
but the codeline only show:
my.app.ExceptionCaughtActivity$1.onClick(ExceptionCaughtActivity.java:25)
android.view.View.performClick(View.java:2486)
android.view.View$PerformClick.run(View.java:9130)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:130)
android.app.ActivityThread.main(ActivityThread.java:3703)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
dalvik.system.NativeStart.main(Native Method)
you notice it not showed the:
"java.lang.NullPointerException"
I konw I do not called the getCause() but when try to use getCause(), it return a null, not a throwable;
I tried to view the source code of throwable class...but got noting
can you tell me why the getCause is null but printStackTrace have a error cause to print out ??