How do you debug Android inEclipse

Try the following:

  • Build the HelloWorld app.

  • Add a log statement to the end of onCreate:

  public void onCreate (Bundle savedInstanceState) {
         super.onCreate (savedInstanceState);
         setContentView (R.layout.main);
         Log.d ("HelloWorldActivity.onCreate ()", "setContentView () completed");
     }
  
  • Place a breakpoint in the log statement.

  • Run the application in the emulator and note that it works step by step to see the LogCat entry in Eclipse.

  • Change HelloWorldActivity to extend from ListActivity instead of Activity.

      public class HelloWorldActivity extends ListActivity {
    
  • Run the application in the emulator and note that it cannot execute the log statement.

My question is NOT why this fails. My question is: how would you decide to debug this failure? All I see in the Eclipse Debug panel is a RuntimeException. I see that LogCat has a bunch of messages, but it is huge, and I searched for it, but I can not find anything to indicate what was wrong or where an exception occurred in my code. I cannot find a way to display a message inside a RuntimeException or a stack trace to find out which line of code raised the exception.

I assume that there should be more efficient ways to use the tools to find errors, but I am new and can not find a better way to debug, except to wrap everything that I encoded in try / catch. I would expect to find a message in LogCat generated by throwing an exception. I would expect the Debug window to allow you to check the contents of exceptions. I am not saying that such methods do not exist, I am saying that it is difficult for me to understand how a beginner is, how to debug and ask what methods exist and how to use them?

So just put:

  • How would you find this error if you did not know what causes it?
  • What methods would you use to find out the cause?
  • How would you know the details of the exception?
  • Generally, how do you detect problems in your Android code using Eclipse?

Numerous suggestions and discussions are welcome. :)

I would include the contents of LogCat, but it is so large that it is not sensible. You should be able to easily reproduce this yourself, so I left it. Maybe there is something in LogCat to help me, but since it is so large and even a small program works, I need a hint on what to look for and how to interpret it, throwing an exception caused by an API call. I see other messages that indicate that something should be in LogCat, which, although it may be true, I find nothing. If you think something should be in LogCat, please run the test yourself and copy the lines into your answer, which I should find.

Thanks.

========

The list of summary methods so far is as follows:

Invasive methods: 1. Place a toast in the code places where you want to see what you have completed. 2. Place a try / catch around the code where you think it is possible to throw an Exception. 3. Comment on the code and recompile and retest.

Non-invasive methods: 1. Use a debugger. Breakpoints, variable checking ... 2. Monkey stress tester. 3. Download the Android source library. 4. Use LogCat filters to see if "Caused By" is displayed.

It is unclear whether: 1. Debugging a version of the Android library that has additional logging, approvals, or other additional help is available. 2. Ability to check exceptions in Eclipse through the Debug panel or other methods. 3. A way to define a more global try / catch exception handler. 4. Ability to debug through the source code of the Android library.

Not available: 1. Non-invasive way to view the contents of the Exception or where the exception occurred.

+6
android debugging eclipse logging exception
source share
4 answers

hey, hysterical question. Well, the first tip, you can filter what logcat says. For example, you can simply show you errors by clicking on the red (e). It also tells you where the error occurred if you run the application in debug mode. It can point directly to your code or to sroid sroid. Knowing which android package caused the error is a big help.

These two just flow into my mind. hope this helps!

+2
source share

I ran into the same problem and found the following Steve H. who helped:

What happens when a debugger is attached, do not send exception logs to LogCat until you terminate the application from within the perspective of debugging. It happens because the application does not actually crash while the debugger disconnects. - Steve H March 31 at 15:47

------ yup, that's all. Now I see the same exception. AFTER I program goes through a complete crash and exit process. It should display this information when it stops my program and displays the IDE debugger screens. Do not leave me bewildered and wasted time with a big click. Eclipse has a long way, it seems to compete with the likes of Visual Studio. I hope my patience exceeds my project. Thanks for the Feedback. :) - Sebastian Janitor Mar 31 at 17:35

Link to the question: What happened to debugging in Eclipse on Android?

+1
source share

Typically, if something throws an exception, then you probably should still be catering for this situation, however, including try / catch blocks is a decent way to find a specific problem. I found that if you do not put something in the catch block, you will not be able to evaluate the exception in the variable clock window in eclipse. Therefore, I always set the log entry and set a breakpoint on this line.

public void onCreate(Bundle savedInstanceState) { try{ super.onCreate(savedInstanceState); setContentView(R.layout.main); }catch(Exception exception) { // put break point on line below so you can evaluate exception in debug mode. Log.e(TAG, "Set content exception "+ exception.getMessage()); // note some exceptions return null on getMessage(); } Log.d("HelloWorldActivity.onCreate()", "setContentView() completed"); } 

So, if your stack trace is huge, this will help. Other developers also found that getting the source code for sdk means that you can see where the error occurs in the main sdk code. I did not do that, though.

0
source share

Exceptions that do not fall into try / catch are errors and disrupt the normal flow of the program.

Starting in debug mode is just a click on the button with an error. I do not know if there are any "special" debug libraries. But when working with Android, all the โ€œlibrariesโ€ are open source, so you can pretty much browse anything.

The good thing about debugging mode is that when an error occurs, your application freezes right in the limb when an error occurs. You can set the set control points, change your code on the fly while your program is running, which is great (well, you cannot make radical changes, for example, change the name of a method).

The way you handle errors and errors in android, however, may be slightly different from .NET, since the model in each of them is different.

When programming on windows, applications work like small islands. You have very direct control over the code stream (i.e. you can call up a modal dialog to freeze the code stream while the user enters some data), and you can make a fully functional program using only one stream. In android, almost everything works in it of its own synchronization. And your application should be ready to handle things like getting a phone call midway through. Thus, you can also apply this model for debugging: Errors (which happen due to unusual circumstances) tend to propagate much more than in other development environments . The way these errors are handled is also different: this is visible when you realize that your application is still running even after throwing an exception.

Some useful tips: You have a very powerful tool called Monkey - a stress tool that generates "pseudo-random streams of user events, such as clicks, touches or gestures," as well as a number of system-level events. "

LogCat indicates the "cause" of the error. The line usually begins with Caused By. If you are interested in a cause, not an effect, you can additionally filter your error reports for "Caused by" searches.

And last but not least, I find the old method of commenting out lines and see what is very useful for figuring out things.

hope this helps

0
source share

All Articles