Displaying MapField inside the application throws IllegalArgumentException Bitmap is too large exception - blackberry

I'm trying to add a map field in my blackberry application, but I get an exception on navigating to that screen saying IllegalArgumentException(Bitmap is too large). Any Idea?
Code:
package com.quadrazol.bb.fpg.screens.maps;
import net.rim.device.api.lbs.maps.ui.MapField;
import net.rim.device.api.lbs.maps.model.MapDataModel;
import net.rim.device.api.lbs.maps.model.MapLocation;
import net.rim.device.api.lbs.maps.model.Mappable;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import com.quadrazol.bb.fpg.screens.TabScreen;
import com.quadrazol.bb.fpg.util.DataHelper;
import com.quadrazol.bb.fpg.util.UIHelper;
public class FindAPlannerScreen extends TabScreen {
private MapField basicMapField;
public FindAPlannerScreen() {
super();
VerticalFieldManager mgr = new VerticalFieldManager(
VerticalFieldManager.USE_ALL_HEIGHT
| VerticalFieldManager.USE_ALL_WIDTH
| VerticalFieldManager.NO_VERTICAL_SCROLL) {
public void paint(Graphics graphics) {
graphics.setBackgroundColor(Color.BLACK);
graphics.clear();
super.paint(graphics);
}
};
add(mgr);
mgr.add(UIHelper.generateHeader());
mgr.add(new SeparatorField() {
public void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
super.paint(graphics);
}
});
VerticalFieldManager mainScrollView = UIHelper.generateMainScrollView();
basicMapField = new MapField(mainScrollView.getWidth()-6,(int) (mainScrollView.getHeight()*.90));
MapDataModel model = basicMapField.getModel();
Mappable[] plannerDetails = DataHelper.fetchPlannerDetails();
if (plannerDetails != null) {
model.addAll(plannerDetails);
}
mainScrollView.add(basicMapField);
mgr.add(mainScrollView);
mgr.add(UIHelper.generateTabBarForScreen(UIHelper.TAB_MAP));
}
}
Exception:
[0.0] ViewEngine$RenderingEngine:unknown exception occured, IllegalArgumentException(Bitmap is too large)
[0.0] IllegalArgumentException
[0.0] Bitmap is too large
[0.0] net_rim_cldc-10(4C48DD41)
[0.0] Bitmap
[0.0] <init>
[0.0] 0x8778
[0.0] net_rim_cldc-10(4C48DD41)
[0.0] Bitmap
[0.0] <init>
[0.0] 0x8628
[0.0] net_rim_cldc-10(4C48DD41)
[0.0] Bitmap
[0.0] <init>
[0.0] 0x8609
[0.0] net_rim_cldc-10(4C48DD41)
[0.0] Bitmap
[0.0] <init>
[0.0] 0x85ED
[0.0] net_rim_cldc-10(4C48DD41)
[0.0] Bitmap
[0.0] <init>
[0.0] 0x859E
[0.0] net_rim_bb_maps_api-4(4C48E231)
[0.0] ViewEngine
[0.0] generateContext
[0.0] 0x6457
[0.0] net_rim_bb_maps_api-4(4C48E231)
[0.0] ViewEngine$RenderingEngine
[0.0] <private>
[0.0] 0x65F2
[0.0] net_rim_bb_maps_api-4(4C48E231)
[0.0] ViewEngine$RenderingEngine
[0.0] run
[0.0] 0x6507
PS: I'm also confused between usage of net.rim.device.api.lbs.maps.ui.MapField and net.rim.device.api.lbs.MapField
Update:
I tried as per your suggestion, but still it is throwing the same error. I noticed one strange thing. The updated code is as follows:
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight();
basicMapField = new MapField(displayWidth / 2, displayHeight / 2);
System.out.println("Display Dimen:"+displayHeight+" "+displayWidth);
System.out.println("Map Dimen:"+basicMapField.getHeight()+" "+basicMapField.getWidth());
But in logs I dont see the parameters set. The logs shows:
[0.0] Display Dimen:480 360
[0.0] Map Dimen:0 0

IllegalArgumentException: Bitmap is too large means that somewhere inside of the RIM's internal code there was an attempt to create a really big Bitmap instance. What is "really big" knows only RIM - there's no clear documentation on this unfortunatelly.
So a general rule not to get in this issue is to somehow control/restrict the dimentions of the Bitmap instance. Note, to get into the issue you don't even need to explicitly create a Bitmap instance, as in your case it is just enough to create some other stuff that implies Bitmap usage. Definitely when you show/draw/paint smth, then a Bitmap is used even if you don't create it explicitly.
From the code you posted I suspect this line may cause the issue:
basicMapField = new MapField(mainScrollView.getWidth()-6,
(int) (mainScrollView.getHeight()*.90));
Do you know what arguments are actually passed to MapField constructor? To test this guess could you try this:
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight();
basicMapField = new MapField(displayWidth / 2, displayHeight / 2);

Related

Uncaught Exception Error: Blackberry 7 Dev

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.
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();
}
Use try catch blocks
try{
//your back ground code here
}
catch(Exception e){
}

Issue in using managers

I want to dispaly some data at the top of the screen and some at the end of the screen,creating two different managers,the issue is after running i cannot see any data on the screen
public NativeScreen() {
super();
LabelField title = new LabelField("Calendar DatePicker",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
hrzManager = new HorizontalFieldManager() {
protected void paintBackground(Graphics graphics) {
graphics.setBackgroundColor(0x0007F5ED);
graphics.clear();
super.paint(graphics);
}
};
hrzManager.add(title);
VerticalFieldManager verticalFieldManagerUpper = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL |
Manager.USE_ALL_HEIGHT | Manager.USE_ALL_WIDTH ) ;
verticalFieldManagerUpper.add(hrzManager);
//Add the manager to the screen.
this.add(verticalFieldManagerUpper);
VerticalFieldManager verticalFieldManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL |
Manager.USE_ALL_HEIGHT | Manager.USE_ALL_WIDTH ) ;
HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH | HorizontalFieldManager.USE_ALL_HEIGHT);
ImageButtonField bitmapField = new ImageButtonField(Bitmap.getBitmapResource("pimdemo_jde.png"), Field.FIELD_BOTTOM);
horizontalFieldManager.add(bitmapField);
ImageButtonField bitmapField1 = new ImageButtonField(Bitmap.getBitmapResource("attachmentdemo_jde.png"), Field.FIELD_BOTTOM);
horizontalFieldManager.add(bitmapField1);
//Add the fields to the manager.
verticalFieldManager.add(horizontalFieldManager);
//Add the manager to the screen.
this.add(verticalFieldManager);
Use the set status and set title -
setTitle(field);
setStatus(field);

Stuck on first attempt at Blackberry App

attempting a first Blackberry App.
It will display diary data (eventually).
I'm just trying to get things working bit by bit.
I can't get the buttons to work in the simulator ie I click them and nothing happens.
Any help appreciated.
Code is below (hopefully ok formatted - first post so apologies if not).
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
/**
* A class extending the MainScreen class.
*/
public class MyScreen extends MainScreen implements FieldChangeListener
{
/**
* Creates a new MyScreen object
*/
ButtonField lastWeek;
ButtonField todayWeek;
ButtonField nextWeek;
LabelField Monday;
LabelField MondayData;
LabelField Tuesday;
LabelField TuesdayData;
LabelField Wednesday;
LabelField WednesdayData;
LabelField Thursday;
LabelField ThursdayData;
LabelField Friday;
LabelField FridayData;
LabelField Satday;
LabelField SaturdayData;
LabelField Sunday;
LabelField SundayData;
public MyScreen(){
LabelField banner = new LabelField("Diary",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField title = new LabelField("Week starting...",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField Monday = new LabelField("Monday",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField MondayData = new LabelField("MondayData",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField Tuesday = new LabelField("Tuesday",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField TuesdayData = new LabelField("TuesdayData",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField Wednesday = new LabelField("Wednesday",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField WednesdayData = new LabelField("WednesdayData",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField Thursday = new LabelField("Thursday",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField ThursdayData = new LabelField("ThursdayData",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField Friday = new LabelField("Friday",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField FridayData = new LabelField("FridayData",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField Saturday = new LabelField("Saturday",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField SaturdayData = new LabelField("SaturdayData",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField Sunday = new LabelField("Sunday",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
LabelField SundayData = new LabelField("Sundaydata",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
HorizontalFieldManager hfm = new HorizontalFieldManager(Field.FIELD_HCENTER);
ButtonField lastWeek = new ButtonField("<<", ButtonField.CONSUME_CLICK);
lastWeek.setChangeListener(this);
ButtonField todayWeek = new ButtonField("Today", ButtonField.CONSUME_CLICK);
todayWeek.setChangeListener(this);
ButtonField nextWeek = new ButtonField(">>", ButtonField.CONSUME_CLICK);
nextWeek.setChangeListener(this);
hfm.add(lastWeek);hfm.add(todayWeek);hfm.add(nextWeek);
hfm.setPadding(10, 0, 10, 0);
VerticalFieldManager vfm = new VerticalFieldManager(Field.FIELD_VCENTER);
vfm.add(Monday);
vfm.add(MondayData);
vfm.add(Tuesday);
vfm.add(TuesdayData);
vfm.add(Wednesday);
vfm.add(WednesdayData);
vfm.add(Thursday);
vfm.add(ThursdayData);
vfm.add(Friday);
vfm.add(FridayData);
vfm.add(Saturday);
vfm.add(SaturdayData);
vfm.add(Sunday);
vfm.add(SundayData);
add(vfm);
add(new SeparatorField());
setTitle(title);
setBanner(banner);
setStatus(hfm);
}
public void fieldChanged(Field field, int context) {
if (field == lastWeek) {
lastTextFields();
}
else if (field == todayWeek) {
todayTextFields();
}
else if (field == nextWeek) {
nextTextFields();
}
}
private void lastTextFields() {
Monday.setText("Monday-old");
MondayData.setText("MondayData-old");
}
public void todayTextFields() {
//Monday.setText("Monday");
// MondayData.setText("MondayData");
Dialog.inform("Today pressed");
}
private void nextTextFields() {
Monday.setText("Monday-new");
MondayData.setText("MondayData-new");
}
}
Since you are running your application on 8520 device simulator that doesn't have touch screen, clicking on the buttons will get you nowhere. There are several options available:
Navigate to desired button by using one of the following methods:
Use the keyboard arrow keys to navigate. Press Enter to "click" on it.
Use your mouse's scroll wheel to navigate and then left click to "click".
Press F12 to turn "trackball mode" on and use your mouse navigate. Then either press Enter or right click when the desired button is selected.
Also check this Use the trackball and other Simulating BlackBerry device interaction manuals.
Alternatively, you can compile your application with JRE 6.0 or higher and pick a use a device simulator that supports touchscreen (9800 Torch, 9930 Bold and etc...).
EDIT
You are initiating local LabelFields and ButtonFields instead of the class' member variable. All class member variable remained uninitialized (e.g. equal null). You should remove the redundant local variable definitions.
Update all your LabelFields and ButtonFields in the following way:
LabelField banner = new LabelField("Diary", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
ButtonField lastWeek = new ButtonField("<<", ButtonField.CONSUME_CLICK);

BlackBerry - Can't foreground a running application

I have a UI application that is set to autorun on startup which spawns a few threads doing some work in the background. It has one screen that displays information about the work the background threads are doing.
class AppName extends UiApplication implements SystemListener2 {
private static AppName app;
public static void main(String[] args) {
app = new AppName();
if (ApplicationManager.getApplicationManager().inStartup()) {
app.addSystemListener(app);
} else {
app.initializeLater();
}
app.enterEventDispatcher();
}
public AppName() {
pushScreen(new InfoScreen());
requestBackground();
}
private void initialize() {
// Spawns some threads doing work in the background
}
private void initializeLater() {
invokeLater(new Runnable() {
public void run() {
initialize();
}
});
}
public void powerUp() {
removeSystemListener(this);
initialize();
}
}
When I run the app in the simulator it works fine. Upon startup everything runs and then when I click the icon the screen is foregrounded and displayed. This is the output from the simulator:
3:20:26.612: AM: Starting AppName
3:20:26.612: AM: AppName already running
3:20:26.612: AM: Foreground is requested: AppName(304)
3:20:26.628: AM: Foreground is set: AppName(304)
However, on the device the screen never displays. This is the device debugger output:
[0.0] Starting AppName
[0.0] AppName already running
As you can see the foreground request is never made. I confirmed this by overriding the UiApplication.activate() method and putting in a System.out message to see if it was being called but it's not getting called.
Does anyone have any idea why this is happening?
try using this
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
public void run() {
UiApplication.getUiApplication().requestForeground();
});
}

Setting a background color to my Blackberry application very basic!

This is my screen:
final class GeneralExpenseViewScreen extends MainScreen {
public GeneralExpenseViewScreen() {
super();
LabelField title = new LabelField("TeamMate TEC | Expenses",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
Background bg = BackgroundFactory.createSolidBackground(0xBDBDDB);
setBackground(bg);
HorizontalFieldManager headerAreaManager = new HorizontalFieldManager();
HorizontalFieldManager filterAreaManager = new HorizontalFieldManager();
HorizontalFieldManager expenseListAreaManager = new HorizontalFieldManager();
HorizontalFieldManager totalAreaManager = new HorizontalFieldManager();
HorizontalFieldManager addNewAreaManager = new HorizontalFieldManager();
add(headerAreaManager);
add(filterAreaManager);
add(expenseListAreaManager);
add(totalAreaManager);
add(addNewAreaManager);
/**Begin form layouts**/
Bitmap headerImage = Bitmap.getBitmapResource("sergioheader.png");
BitmapField header = new BitmapField(headerImage);
headerAreaManager.add(header);
}
public boolean onClose() {
Dialog.alert("AH!");
System.exit(0);
return true;
}
}
Notice that I'm calling setBackground directly to the class but it's not working how I think it would work.
How can I set a background color to my application form?
Thanks.
I've used this code with success:
protected void paint(Graphics graphics) {
graphics.setBackgroundColor(0xBDBDDB);
graphics.clear();
super.paint(graphics);
}
Depending on the version you're developing for, you could use the following
getMainManager().setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
to set the screen managers background color.

Resources