Reputation: 367
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.
Upvotes: 0
Views: 131
Reputation: 2650
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:
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();
}
Upvotes: 1
Reputation: 1874
Use try catch blocks
try{
//your back ground code here
}
catch(Exception e){
}
Upvotes: 0