Home Tutorials Hands on Debugging with Eclipse
Debugging with Eclipse
Tutorials - Hands on
Tuesday, 07 October 2008 10:11
Article Index
Debugging with Eclipse
Breakpoints
All Pages

DebuggingThere are a few different ways to write and debug Android software. A popular choice is to use the Eclipse development framework with the Android Eclipse plugin. This allows you to manage your projects in a very convenient way, and launch the Android Emulator directly from the same setup. You'll find the process usually goes edit | launch | curse | debug | stop and edit again, so you'll want something that makes this cycle as painless as possible.

 

Tutorial: Debugging | Skill: Medium | Download apk | Download source

This is what the app looks like when run - nothing more than the default "Hello, World" one changed to show the current time:

 

 

Here's a step-by-step guide, for those new to Eclipse, to getting an Android application up and running and debugging it.

 

  1. (Optional) Watch a video of the Eclipse installation process plus pre-requsites here.
  2. Download and install Eclipse from here, choose the Eclipse IDE for Java Developers.
  3. Install the Android SDK to your computer, then the Eclipse plugin according to this guide.
  4. Choose File | New | Android project
  5. Name the project HelloAndroid
  6. Enter a package name of "com.android.hello"
  7. Use "HelloAndroid" for the Activity name
  8. Use "hello, Android" for the Application name.

     

So far this is exactly what Google suggest for your first project. You will notice some files appear in the Eclipse Package Explorer, and should run the project now to make sure everything is ok by right clicking the project name (HelloAndroid) in the Package Explorer pane, and choosing Run As | Android application from the menu which then appears. You should see the emulator with the basic Hello, World app which just shows that text on a black background.

To keep things simple with the debugger, we'll just make some smaller changes to the main source generated so far. To see some of our changes in the real app, we can use the setTitle(...) call, which just puts our entry in the title window to show we really are exercising our code. So, begin by editing HelloAndroid.java and after the line:

 

setContentView(R.layout.main);

 

Add the lines:

 

String dateAndTime = 
 (new SimpleDateFormat("dd MMM yyyy H:mm:ss a")).format(new Date()); 
 
 setTitle(dateAndTime);

 

And press Ctrl-Shift-O (that's the letter 'O', to 'O'rganise the imports). At the popup box, choose java.utils.Date. This imports the date packages needed by the lines we just added. So the whole file should look like this:

 

/*
 * Copyright (c) 2009 OTAMate Technology Ltd. All Rights Reserved.
 * http://www.androidacademy.com
 */
 
package com.androidacademy.tutorials.debugging;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import android.app.Activity;
import android.os.Bundle;
 
/**
 * Extremely basic tutorial to illustrate debugging.
 */
 
public class Main extends Activity {
 
  /**
   * Called when the activity is first created.
   * Create a basic Android application which sets the
   * title to the current time, using a variable we
   * can use to debug.
   */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String dateAndTime = "The time is " +
          (new SimpleDateFormat("H:mm:ss a")).format(new Date());
 
        setTitle(dateAndTime);
        setContentView(R.layout.main);
    }
}


 

Add comment


Security code
Refresh


Copyright © 2010 Android Academy. All Rights Reserved. Privacy statement. Sitemap.
Android Academy is not associated with Google in any way. All trademarks acknowledged.