Lesson 2: Writing and testing your BlackBerry application

In this lesson, you create a class with a main method that opens a HomeScreen class, which contains a title and a status message.

 Write and test a sample application in Eclipse
  1. Add an Application class to your project.

    1. In the Package Explorer window, expand HelloBlackBerry and click src.

    2. Click File » New » Class.

      The New Java Class window appears.

    3. In the Name field, type Application.

    4. Under the Which Method Stubs Would You Like To Create option, select Public Static Void Main([String() Args]).

    5. Click Finish.

      The Application.java file appears under your project in the Package Explorer window.

  2. Modify the Application class.

    Double-click Application.java in the Package Explorer window, and then add a constructor and a main method.

    Your Application.java code should look like the following code:



    class Application extends net.rim.device.api.ui.UiApplication {
        public static void main( String[] args )
        {
            Application instance = new Application();
            instance.enterEventDispatcher();
        }
        Application() {
            pushScreen( new HomeScreen() );
        }
    }
  3. Add a HomeScreen class to your project.

    1. In the Package Explorer window, expand HelloBlackBerry and click src.

    2. Click File » New » Class.

      The New Java Class window appears.

    3. In the Name field, type HomeScreen.

    4. Click Finish.

      The HomeScreen.java file appears under your project in the Package Explorer window.

  4. Modify the HomeScreen class so that it displays a title and status messages.

    Double-click HomeScreen.java in the Package Explorer window, and then update the code so that it displays a title and a status message.

    Your HomeScreen.java code should look like the following code:



    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    import java.util.*;
    
    class HomeScreen extends MainScreen {
    
        HomeScreen() {
            
            // Set the window title
            LabelField applicationTitle = new LabelField("Hello BlackBerry");
            setTitle(applicationTitle);
            
            // Add a label to show application status
            _statusLabel = new LabelField( "Status: Started" );
            add( _statusLabel );
        }
        private LabelField _statusLabel;
    }

    The _statusLabel is defined as a class variable so that it can be accessed from other parts of the application later.

  5. Run the simulator.

    In the Package Explorer window, click Application.java, and then click Run » Run As » BlackBerry Simulator.

    Note

    If multiple projects are open in your workspace, click Run » Run Configurations, select HelloBlackBerry, and then click Run.

    The HelloBlackBerry project compiles and then the simulator window appears.

    Ensure that the project compiles without errors by selecting the Problems tab in Eclipse.

  6. From the simulator menu, click Simulate » Set IT Policy.

    The Set IT Policy window appears.

  7. In the Policy field, click Allow Third Party Apps To Use Persistent Store » >>.

  8. Click Set and then click Close.

  9. Launch your application.

    In the simulator window, navigate to Downloads and then run the HelloBlackBerry application.

    A screen appears that displays the Hello BlackBerry title bar and the Status: Started text.

  10. Stop the simulation.

    In the simulator window, click File » Exit.