0

I do not understand what it is that I'm doing wrong, the code seems alright, I am trying to set a background image for a GridLayout, when I add the background code, I get an error, when I comment it out, it runs good in the simulator.. What am I doing wrong?

UPDATE

   public class GFMScreen extends MainScreen {

    VerticalFieldManager ver;
    ButtonField submit;
    HorizontalFieldManager hr;
    private int phoneWidth = Display.getWidth();
    private int cellHeight = Display.getHeight();

    public GFMScreen() {
        super(Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL);
        setTitle("Jonezing");
        createGUI();
    }

    public void createGUI() {
        try {
            final Bitmap background = Bitmap.getBitmapResource("bg.png");
            ver = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL
                    | VerticalFieldManager.NO_VERTICAL_SCROLL
                    | Manager.FIELD_VCENTER | USE_ALL_HEIGHT | USE_ALL_WIDTH) {

                public void paint(Graphics g) {
                    Bitmap scaled = new Bitmap(phoneWidth, cellHeight);
                    background.scaleInto(scaled, Bitmap.SCALE_TO_FIT);
                    g.drawBitmap(0, 0, phoneWidth, cellHeight, scaled, 0, 0);
                    super.paint(g);
                }
            };
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // ver.setBackground(BackgroundFactory.createBitmapBackground(Bitmap
        // .getBitmapResource("bg.png")));
        ver = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL
                | VerticalFieldManager.NO_VERTICAL_SCROLL
                | Manager.FIELD_VCENTER | USE_ALL_HEIGHT | USE_ALL_WIDTH);
        GridFieldManager grid = new GridFieldManager(4, 2,
                Manager.FIELD_VCENTER | Manager.NO_VERTICAL_SCROLL);
        grid.add(new BitmapField(Bitmap.getBitmapResource("english.png")));
        grid.add(new BitmapField(Bitmap.getBitmapResource("yoruba.png")));
        grid.add(new LabelField("English", Field.FOCUSABLE));
        grid.add(new LabelField("Yoruba", Field.FOCUSABLE));
        grid.add(new BitmapField(Bitmap.getBitmapResource("mp.png")));
        grid.add(new BitmapField(Bitmap.getBitmapResource("about.png")));
        grid.add(new LabelField("MP3s", Field.FOCUSABLE));
        grid.add(new LabelField("About", Field.FOCUSABLE));
        grid.setPadding(100, 0, 50, 100);
        grid.setColumnPadding(100);
        grid.setRowPadding(10);
        ver.add(grid);
        add(ver);
    }
    }

Thank you.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
  • You would do better if you analysed your problem more and gave us more information. For example, this is not a problem description "when I add the background code, I get an error, when I comment it out, it runs good". What is the error? That said - please see answer below. – Peter Strange Mar 17 '14 at 10:09

2 Answers2

1

There is a possibility that your error is related to GridFieldmanager requiring to be in a non scrolling Manager. So you will get a runtime exception, can't remember what it is, but most likely an IllegalStateException.

I have attempted to use GridFieldManager in the past, and to be honest, I have given up on it, because of problems like this.

As I recommended in your other question here:

blackberry-development-ui

I would stop using GridFieldManager and use TableLayoutManager instead. You can do the same things and if you have any problems with TableLayoutManager, you have the code and can fix it.

If you really want to use GridFieldManager, then find an example of it being used that works, copy that code exactly, and change it bit at a time until you get it to the state you want it.

The other possibility is that the code can't find the background image, in which case you are getting a NullPointerException in the paint() method. Because your problem description is not very good, we can't tell. To check this, just see if the result of this statement:

final Bitmap background = Bitmap.getBitmapResource("bg.png");

has the Bitmap background set to null or not. If it is NOT null, then the image is being found. If it is null, then we need to look for another resolution.

Finally, can I recommend that you do as little processing in paint() as you can. In the paint() method for your VerticalFieldManager, you actually scale the Bitmap every time. Now paint() gets called a lot and scaling is an expensive operation, so I recommend that you rework this code so that you scale the Bitmap only one.

Sorry one more thing - you may need to be careful about image scaling. You are scaling an image to match the size of the screen - what if that screen is landscape or portrait? Will the image become distorted?

Update

I have just noted a couple of other things with this code:

try {
    final Bitmap background = Bitmap.getBitmapResource("bg.png");
    ver = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL
            | VerticalFieldManager.NO_VERTICAL_SCROLL
            | Manager.FIELD_VCENTER | USE_ALL_HEIGHT | USE_ALL_WIDTH) {

        public void paint(Graphics g) {
            Bitmap scaled = new Bitmap(phoneWidth, cellHeight);
            background.scaleInto(scaled, Bitmap.SCALE_TO_FIT);
            g.drawBitmap(0, 0, phoneWidth, cellHeight, scaled, 0, 0);
            super.paint(g);
        }
    };
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

1) In this code, the try/catch is only catching errors in the creation of the bitmap and the VFM. It does not catch any errors in the paint() method. I doubt very much that you are getting an error in the constructions of either the bitmap or the VFM

2) The e.printStackTrace(); will not produce a stack trace, in fact in this case I doubt that it will do anything at all, except mask a problem. The only way to get a stack trace in BB java is to catch Throwable. the code you probably want to use goes like this:

    try {
....
    } catch (Throwable t) {
        t.printStackTrace();
    }
Community
  • 1
  • 1
Peter Strange
  • 2,640
  • 11
  • 11
0

Use try catch blocks

try{
//your back ground code here

}

catch(Exception e){

}

RKC
  • 1,834
  • 13
  • 13
  • App runs fine, but no background is showing.. itz white – Oluleye IResþekt Idowu Mar 17 '14 at 09:45
  • If "bg.png" is present in the same folder as that of your .java file then the given path is fine ,otherwise you need to mention the correct path of the background image. – RKC Mar 17 '14 at 09:57
  • the bg.png is in the res folder along with the other PNGs (engish, yoruba, about and mp) as in the code above, I will try putting it in the same folder as the java file. – Oluleye IResþekt Idowu Mar 17 '14 at 10:02
  • I have put the bg.png in the same folder as the the java file, seems its in my code. I will update my post above so you can help check. Thanks. – Oluleye IResþekt Idowu Mar 17 '14 at 10:09
  • In graphics.drawBitmap() use "final Bitmap background" which you created.I think this will solve the issue. – RKC Mar 17 '14 at 10:19
  • In graphics.drawBitmap() use "final Bitmap background" which you created.I think this will solve the issue. – RKC 21 mins ago (Done that already, nothing shows) Check my UPDATED code. – Oluleye IResþekt Idowu Mar 17 '14 at 10:40