0

我是黑莓开发新手,想将图像添加到示例黑莓应用程序。我尝试了多个教程,但图像未在后台显示

谁能告诉我是什么问题?

/**
 * This class extends the UiApplication class, providing a graphical user
 * interface.
 */
public class Diverse extends UiApplication {

    private Bitmap backgroundBitmap;
    private Bitmap fieldBitmap;


    public static void main(String[] args) {

        Diverse diverse = new Diverse();
        diverse.enterEventDispatcher();
    }

    /**
     * Creates a new MyApp object
     */
    public Diverse() {
        //The background image.
        backgroundBitmap = Bitmap.getBitmapResource("background.png");

        MainScreen mainScreen = new MainScreen();

        HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH | HorizontalFieldManager.USE_ALL_HEIGHT){

            //Override the paint method to draw the background image.
            public void paint(Graphics graphics)
            {
                //Draw the background image and then call paint.
                graphics.drawBitmap(0, 0, 240, 240, backgroundBitmap, 0, 0);
                super.paint(graphics);
            }            

        };

        //The LabelField will show up through the transparent image.
        LabelField labelField = new LabelField("This is a label");

        //A bitmap field with a transparent image.
        //The background image will show up through the transparent BitMapField image.
        BitmapField bitmapField = new BitmapField(Bitmap.getBitmapResource("field.png"));

        //Add the manager to the screen.
        mainScreen.add(horizontalFieldManager);

        //Add the fields to the manager.
        horizontalFieldManager.add(labelField);
        horizontalFieldManager.add(bitmapField);

        // Push a screen onto the UI stack for rendering.
        pushScreen(new DiverseScreen());
    }
}

DiverseScreen类是

package diverse;

import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class DiverseScreen extends MainScreen
{
    /**
     * Creates a new MyScreen object
     */
    public DiverseScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("Diverse");
    }
}
4

1 回答 1

1

The problem is that you have set the background image for one screen, but you never displayed that screen. Then, you displayed a different screen that did not have a background image set.

First, it helps to understand the BlackBerry UI framework. It allows you to create a hierarchy of objects on screen. At the top level, you have a Screen (or subclass of Screen), and then inside of the Screen, you have Managers, and inside them are Fields. But, each level must be added to a container of some kind, and finally, the top-level Screen must be displayed with something like pushScreen().

See a little more description on this hierarchy in a recent answer here

In your situation, you should change this line

    MainScreen mainScreen = new MainScreen();

to

    DiverseScreen mainScreen = new DiverseScreen();

and then change this line

    pushScreen(new DiverseScreen());

to

    pushScreen(mainScreen);

since mainScreen is the instance where you added a horizontal field manager that draws your background image.

于 2013-05-02T04:37:04.697 回答