Locating Errors When Developing Android Apps - android

I'm working on my first Android app and to be honest I'm not sure about most of what I'm doing. Right now I'm stuck on a NullPointerException that is created by a line that refers to another class that, in turn, refers to another class.
How can I locate the error?

The word you are looking for is debug. If you are using eclipse, it's very easy to debug your program in most cases. Two main options in eclipse are to use the logger for debug prints to logcat, or debug the program step by step to detect relevant errors. Here is a tutorial for both options and here is a nice video tutorial in YouTube regarding debug in eclipse.

You can use the Eclipse debugger to help with that. Set a break point above the line that errors out and deploy your app using the Eclipse debugger (with the little bug icon) rather than the standard deployer (the play button). You'll be able to see what is going on right before the line with the error occurs and hopefully fix things up.
If that doesn't work, you can post the stack trace and your method that has the error and we can take a look at it.

Related

How do you debug successfully in Android?

I understand there is the LogCat view to check the messages logged but I can't make sense of it.
When debugging (I use Java primarily) I've been accustomed to stepping over each line of code and finding out the exact point where the program crashes and then doing something about it.
How can I do this with Android development? Is there a way to precisely locate the line which is causing the application to crash?
I can't understand what to make of/how to read the LogCat messages and when I try to step over (using the F9 key or the 'Debug' option in Eclipse) it keeps opening new tabs for inbuilt classfiles (like Instrumentation.class etc) and after pressing F6 a few times over again the app crashes showing 'The application has stopped unexpectedly. Please try again'
Can someone tell me if there's something to be done in a way that I'm not doing here?
Thanks!
Btw if it helps, here's the generated log:
http://pastebin.com/EaqaWUdS
You are using a resource id that doesn't exist at line : 93 of com.site.custom.ModAdapter.getView(CustomListProjectActivity.java
--EDIT : add explanations
You will read a logcat stack trace in the same way as you did in Java : read bottom up and the culprit is the last line of your classes (before the code gets into the android sdk code).
You can do it the other way around, and start from top, stopping at the first class of yours and discarding android classes.
The same reasoning applies when debugging : step into your methods if needed and step over all methods of the SDK unless you want to debug them (and usually you don't, if you really suspect a bug inside the SDK, check the source at grepcode to see the inner mechanics of the android sdk class you are interested in).
Sometimes it gets difficult to track bugs on android, especially for widget layout related bugs because you can only see the code that is executed by the android platform, no code of your is executed, only your data are read from an xml file for instance. If something breaks here, it can be harder to debug. In that case, apply the dichotomy method : remove half line, if the bug doesn't show up, then readd your lines, remove half of them, etc...
It is the same like in java. Basically you need the sources to open the java files instead the class files. Google shows you how to add the sources.
Basically you debug android while staying in your own classes. I barely look into the android classes as the most issues are, of course, located in my own classes.
So just debug like you already do but don't step into methods/classes you don't own unless you have the sources added to your sdk. Even if you have, there might be some classes that aren't open source, so you can't step into the sources there. (Basically all Google API classes)

Android debugging problems

I am very new to Android and Eclipse. I find very difficult to fix up the errors in Eclipse. Android emulator keeps crashing, even for few lines of codes. Is there any tutorial or video tutorial available, that can help me to spot errors on looking at the error log.
Here is a basic tutorial that will get you familiar with Android Debugging and Logcat : Debugging in Android using Eclipse . Also you can set breakpoints and debug as you would for any other Eclipse project. The logging of errors, its different tough, using Logcat.
Yes, you can open DDMS perspective and LogCat window to view logs and can see what is the original issue.
When it crashes, check the LogCat. It contains the stack trace with the exception, and is often enough to see what went wrong and fix it. It can be browsed directly in Eclipse (don't know exactly how, I'm using IntelliJ).
Also, you can use normal debugging on your Android project, exactly the same way you would on a Java (non-Android) project.

No source attachment error when debugging Android applications

I have just started doing Android development on Mac OS X in Eclipse. When debugging an Activity, I keep getting the following errors:
The JAR file /platforms/android-10/android.jar has no source attachment.
This is starting to get very annoying, does it mean something is throwing an exception somewhere? (the message itself does not give any meaningful information as to why this is happening) I do not want to step into the source; does Eclipse do this by default? How do I disable it?
It looks like you have some error in your application. And Android subsystem throws exception. Normally, if such exception originates from android, you'll see this behavior.
What you should do:
Turn on LogCat view to see logs.
Launch your app without debugger (Ctrl+F11 on ubuntu/windows)
Inspect those logs in LogCat very carefully. They will contain the place where exception happened (originating from your code). You will then easily be able to fix the issue yourself (or ask for more assistance if needed).
In a normal development workflow you shouldn't hit cases like yours too often. Its just the learning curve :) So stay calm and keep learning.

How to debug android application using android sdk?

I am new to android application development, I am finding it difficult to debug my program I am always getting some abstract message like "your application stopped working" I need to know exactly. I have read there are many ways to debug. What I need to know is the way which will be easy for beginners like me.
If you are using eclipse (if not you should, is the easiest way to develop/debug for android) take a look at these tutorials, specially the second one (there is a lot more out there if you google a little):
http://www.latenightpc.com/blog/archives/2007/11/21/starting-a-debug-session-for-android-with-adt
the main point you should research about is LogCat and debugger (both covered in the above links)
In eclipse always look at the logCat output in the DDMS or Debug window when the app crashes. Often (buried in the output) is a line telling you exactly what statement (file and line number) caused the crash. Sometimes you need to hit resume (F8) in the Debug window to get the output. Once you get that info you could set a breakpoint at the offending statement and then look at what variable(s) are messed up.

Android - Having a hard time debuging Eclipse

I am a little bit new to developing for Android using Eclipse (Coming from a .NET/Visual Studio background).
My biggest problem in developing Android app is "debugging" them. Every time the emulator throws an error, there is no message, no explanation of what caused the error. I basically have to keep doing trial/error until something works.
My question is: Is there a better way to analyze the error messages that emulator shows (basically some stack trace about Dalvik)!
Also, is it possible in Eclipse [when debugging] to move the debugging cursor backwards to re-evaluate a variable or "skip" some lines of code?
Use LogCat view to see error/debugging messages. Regular console is of very limited help. Window->Show View->Other->Android->LogCat There on the top you will see some round buttons that basically can filter log from V (verbose) to E (error). You can't copy/paste from theLogCat window but you can save selected output to text file
Regarding the error messages (I'm assuming these are the messages that show up in LogCat): I've found that analysing the error messages and working your way up the list until you reach code that is yours is the most effective way to work. Frequently (for me at least) the emulator will crash and break somewhere in the OS code (which you probably don't have the source for) but you can follow the LogCat messages back to your code and start to see where the problem is. The crash may appear to happen in the OS code, but the original cause is typically you passing something wrong to the OS.
Regarding going backwards in code: nope, at least not with the my Eclipse Ganymede install
Regarding skipping code: When your in debug mode under the Run menu you can select "Step Over" (skips going into a method) , "Step Return" to exit a method and "Run to Line" in addition to setting Breakpoints by right clicking. This causes the code to run, but you aren't single stepping through it. Otherwise the only way to "skip" lines of code is to comment it out. I believe you can also change the value of variables in the debugger, but I've never tried personally. This may give you the desired effect.
Try closing all unrelated project, switch off eclipse and then do a clean and build for the project in picture.

Categories

Resources