I am building an app but on running my code i keep getting a nullpointer exception when i debug it i dont seem to notice what could possibly be giving it an error,i get it on the coated line , do i need to put the class of this method for a better explanation ?
Thanks
protected void paint(Graphics graphics) {
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, getWidth(), getHeight());
// graphics.drawBitmap(xInt, yInt, imgWidth, imgHeight, _currentPicture, 0, 0);
}
The only thing on that line that I can see that would be causing a NullPointerException would be if _currentPicture were null. You could test the theory by changing your code to:
protected void paint(Graphics graphics) {
if(_currentPicture != null) {
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics
.drawBitMap(xInt, yInt, imgWidth, imgHeight, _currentPicture, 0, 0);
}
}
The variable _currentPicture isn't initialized. Look farther back in your code and see where it gets set. We can't do much more without more code or the full stacktrace.
Related
I want the background image to stretch all screen. I simply created a VerticalFieldManager and modified paint method.
verticalFieldManager = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH
| VerticalFieldManager.NO_HORIZONTAL_SCROLLBAR) {
public void paint(Graphics graphics) {
graphics.drawBitmap(0, 0, this.getWidth(), this.getHeight(),
backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
And when i add a ListField to the verticalFieldManager and scroll, background image is not repaint. How can i repaint background when scroll?
Create EncodedImage from your Bitmap, or even directly extract EncodedImage from resources - EncodedImage.getEncodedImageResource("res/bg.png").
Then use this to scale it:
public static EncodedImage resize(EncodedImage eImage, int toWidth,
int toHeight, boolean keepAspectRatio) {
int scaleX = Fixed32.div(
Fixed32.toFP(eImage.getWidth()),
Fixed32.toFP(toWidth)
);
int scaleY = Fixed32.div(
Fixed32.toFP(eImage.getHeight()),
Fixed32.toFP(toHeight)
);
if (keepAspectRatio) {
int scale = (scaleX > scaleY) ? scaleX : scaleY;
return eImage.scaleImage32(scale, scale);
} else {
return eImage.scaleImage32(scaleX, scaleY);
}
}
Then you may draw the EncodedImage on the Graphics object:
graphics.drawImage(
0, 0,
encodedImage.getScaledWidth(), ncodedImage.getScaledHeight(),
encodedImage, 0, 0, 0
);
I know, this question was asked before. However I can't beleive that this operation is so difficult. I think i miss some important points on developing Blackberry applications.
Try to use the following code:
Bitmap backgroundBitmap = Bitmap.getBitmapResource("background.png");
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 super.paint
//to paint the rest of the screen.
graphics.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(),
backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
how i set background image in java blackberry simulator ?
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);
}
};
try this blog post:
How to set Background Image in Blackberry
Or this support forum thread:
drawing bitmap in Mainscreen background
Bitmap bitmapOrig = Bitmap.getBitmapResource("ICON.png");
// Create a Bitmap of arbitrary size
int scaledX = 360;
int scaledY = 415;
Bitmap bitmapScaled = new Bitmap(scaledX, scaledY);
bitmapOrig.scaleInto(bitmapScaled , Bitmap.FILTER_LANCZOS);
bitmapOrig.scaleInto(bitmapScaled , Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
BitmapField bitmapFieldScaled2 = new BitmapField(bitmapScaled , Field.FOCUSABLE);
homeScreen.add(bitmapFieldScaled2);
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);
}
protected void sublayout(int maxWidth, int maxHeight){
super.sublayout(240,240);
setExtent(240,240);
}
};
Need lil help regarding paint method in MainScreen. Using the code above, I was trying to render some lists. It works fine. But it hides all my status bar icons i added using setStatus() method. When I remove the paint method , my buttons in status bar shows up. Also I have tried using setRe g.pushRegion() , but no luck.
Here is sample code :
private void addStatusBar(){
manager = new HorizontalFieldManager();
manager.add(new BitmapField(ImageUtility.loadBitMap("ask.jpg")));
manager.add(new BitmapField(ImageUtility.loadBitMap("experts.jpg")));
manager.add(new BitmapField(ImageUtility.loadBitMap("search.jpg")));
manager.add(new BitmapField(ImageUtility.loadBitMap("my_profile.jpg")));
manager.add(new BitmapField(ImageUtility.loadBitMap("groups.jpg")));
manager.add(new BitmapField(ImageUtility.loadBitMap("analitics.jpg")));
setStatus(manager);
}
protected void paint(Graphics graphics) {
//graphics.pushRegion(new XYRect(0, 0, getPreferredWidth(), getPreferredHeight()));
Bitmap image = ImageUtility.loadBitMap("header2.jpg");
graphics.drawBitmap(0, 0, 500, image.getHeight(), image, 0, 0);
for(int i =0; i < 5; i++ ){
fieldList.drawListRow(fieldList, graphics, i, 50 + (i*50), 20);
}
}
Have you tried setting the "y" offset for the graphics.drawBitmap() call to the height of the status bar?
you need to call super.paint Otherwise the status is never painted.
protected void paint(Graphics graphics) {
Bitmap image = ImageUtility.loadBitMap("header2.jpg");
graphics.drawBitmap(0, 0, 500, image.getHeight(), image, 0, 0);
for(int i =0; i < 5; i++ ){
fieldList.drawListRow(fieldList, graphics, i, 50 + (i*50), 20);
}
super.paint(graphics); }
in my blackberry application i have to create a effect in which fullscreen bitmap is slowly disappearing and ui screen is coming up.
protected void paint(Graphics g) {
g.setGlobalAlpha(globalAlpha);//starting value of globalAlpha is 255.
g.drawBitmap(0, 0, getWidth(), getHeight(), _bitmap, 0, 0);
g.setGlobalAlpha(255 - globalAlpha);
globalAlpha--;
super.paint(g);
}
This code is just for giving demo that what i want.
super.paint(g) is calling 255 times because of that its a poor code.
in one timer task i am calling invalidate();
So any suggestions how to implement this?
If you can get away with using 5.0 APIs, you'll save yourself a LOT of work by using the TransitionContext class. One of the transition types is "fade".