views:

349

answers:

3

I've just started debugging my first three line long android app and I can't seem to use the debug tool like I want to. Here's my code:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  int a = 1 / 0;
}

Now I expect the debugger to halt the thread and show me the line number of statement where the division by zero occurs. No, instead it shows some other method internal to the system for which I have no source. To make the matters worse, there is no exception message either.

Prior to this app, I created one which would do something when a button was pressed. If any exception was raised, again no useful line number or exception message would be shown.

As of right now, there is no way to debug my app. Any ideas?

I'm using the latest SDK along with Eclipse ADT plugin and debugging on a real device (Nexus One).

+1  A: 

Eclipse doesn't debug like that. You need to use LogCat to see the actual error message and then figure out where it happened. I can't remember if LogCat shows a line number.

Eclipsed4utoo
well, that's a bummer - you mean to say the whole eclipse native debugging is there for no reason?And yes, there are line numbers in LogCat (thankfully).
mikeY
You can set breakpoints, which is really what debugging means.
Eclipsed4utoo
A: 

Debugging does not mean that you will be presented a line number and exception text when "something" happens... Debugging means to be able to set breakpoints, hold your code at specific points and possibly do step-by-step instructions, watch variables, etc.

To detect problems like that, you as a programmer will have to implement useful log texts using Log class, for example when exiting and entering functions. Also, use exception blocks wherever you suspect things to possibly go wrong. In case of a division for example, if the divisor is not static but could be anything, protect actions like divisions! In your catch block, log the encountered event or do something useful.

Basically that is nothing Android specific, but common to Java!

Zordid
I don't mean to start an argument, but out of all different development environment I've used over the years (C, C++, Python, Java for Desktop, Javascript, VB, .NET), being able to know where exactly an exception occured, its details, backtracing and viewing local variables at the time of exception, were all a central part of the debugging process. Debugging is certainly not JUST that, but it's clear this a shortcoming of debugging android apps.
mikeY
You obviously broke the script, but I don't think you understand how to debug in the environment. You can identify an error occurred, now add you tries, breakpoints, and outputs and actually break it down. This is debugging, having the compiler or runtime throw an exception without properly handling it just tells you you did something wrong. Debugging it is your proactive approach at getting rid of the bug.
John Giotta
+1  A: 

At first I have to admit that you are partially right. There are debuggers that will stop the execution at a exception and show your code line that caused it. I would love to see this behavior in the eclipse debugger. But the other answers are right.

While you are in Eclipse go to Window -> Show View -> Other -> Android -> LogCat Now you will get all the debugging output that occurs on the emulator or a connected device. With you example I will get the following StackTrace.

T03-18 09:45:12.398: ERROR/AndroidRuntime(1778): Uncaught handler: thread main exiting due to uncaught exception
03-18 09:45:12.428: ERROR/AndroidRuntime(1778): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.client/android.client.ClientMain}: java.lang.ArithmeticException: divide by zero
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at android.os.Looper.loop(Looper.java:123)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at android.app.ActivityThread.main(ActivityThread.java:4203)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at java.lang.reflect.Method.invoke(Method.java:521)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at dalvik.system.NativeStart.main(Native Method)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778): Caused by: java.lang.ArithmeticException: divide by zero
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at android.client.ClientMain.onCreate(ClientMain.java:35)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     ... 11 more
03-18 09:45:12.438: INFO/Process(52): Sending signal. PID: 1778 SIG: 3

If you go to the deepest Exception shown that one that raised all the others you will see

03-18 09:45:12.428: ERROR/AndroidRuntime(1778): Caused by: java.lang.ArithmeticException: divide by zero
03-18 09:45:12.428: ERROR/AndroidRuntime(1778):     at android.client.ClientMain.onCreate(ClientMain.java:35)

This is pretty clear I think. On line 35 in the ClientMain an Exception was thrown and it was a divide by zero exception. If you can't figure this out (in a case not as clear as the example) you can set a breakpoint on this line or the entry point of the method or something. Now the debugger will show you all the variables and you can step execute the code step by step until the error occurs. If you hover over a varibale you can see the value of this variable and now you can step by step try to understand the cause of the exception and solve it. If you step to deeply into the code you will end in the java class that is responsible for doing the division, if you haven't added the jars with the source code of this classes to your project the debugger isn't able to show you something at this point.

Janusz