While developing Android application, from time to time I face a situation when an exception occures, but can not be traced down through a stack, because the stack which is shown at such moments mentions only system methods, and not any of my code lines. For example, if I erroneusly pass an incorrect string to Float.parseFloat, I got something like this:
Thread [<1> main] (Suspended)
LoadedApk$ReceiverDispatcher$Args.run() line: 710
ActivityThread$H(Handler).handleCallback(Message) line: 587
ActivityThread$H(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3729
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 874
ZygoteInit.main(String[]) line: 632
NativeStart.main(String[]) line: not available [native method]
and this:
Thread [<1> main] (Suspended (exception RuntimeException))
LoadedApk$ReceiverDispatcher$Args.run() line: 722
ActivityThread$H(Handler).handleCallback(Message) line: 587
ActivityThread$H(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3729
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 874
ZygoteInit.main(String[]) line: 632
NativeStart.main(String[]) line: not available [native method]
The question is – how can I pinpoint a line of my code, which causes the problem? In the case of incorrect parseFloat usage, I’d expect to see a mention of parseFloat in the stack and my methods which contain the invocation.
Check Logcat in the DDMS view in eclipse. That will show you a better stacktrace of where exactly in your code the error is thrown and what the error is
Ex. I wrote a test app that uses Float.parseFloat invalidly like you said and get the following error in logcat
We see the error is caused by
java.lang.NumberFormatExceptionand drilling down we see it comes from here:
It points to my activity in the method onCreate on line #15. If i look at line 15 I see
Float.parseFloat(“bacon”); and find my problem.
We can’t parse bacon as a float (Although that may be delicious) so it throws the error.