Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Posts: 17
Registered: ‎04-09-2012
My Device: BB9900
My Carrier: 02

Unusual java uncaught outofmemory error.

[ Edited ]


I am having a problem I can't seem to fix. In the simulator, i open my app and close it, I do this a few times (purposely, for testing) and after maybe the 4th time, I get an outofmemory error. Is it that my app is not closing well enough, or there are objects I'm not ending or what? I tried it on other apps on the simulator (like facebook and twitter) and I get no outofmemory.


public class MyApp extends UiApplication
     * Entry point for application
     * @param args Command line arguments (not used)
    public static void main(String[] args)
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MyApp theApp = new MyApp();       

     * Creates a new MyApp object
    public MyApp()
        // Push a screen onto the UI stack for rendering.
        pushScreen(new PictureScrollFieldScreen());


Here is the mainscreen you see when you open the app

package mypackage;

import utils.GafaUtils;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.extension.component.PictureScrollField;
import net.rim.device.api.ui.extension.component.PictureScrollField.HighlightStyle;
import net.rim.device.api.ui.extension.component.PictureScrollField.ScrollEntry;

public class PictureScrollFieldScreen extends AScreen{
	private VerticalFieldManager _manager;
	HorizontalFieldManager _hfm, _hfmbottomcontainer;
	VerticalFieldManager _vfmleft, _vfmright;
	PictureScrollField _pictureScrollField;
	int  _containerheight, _containerwidth;
	Bitmap _homeimage;
	public PictureScrollFieldScreen(){ 

		_manager = (VerticalFieldManager)getMainManager();
		_containerwidth = (Display.getWidth()/3) * 2;
        Bitmap[] images = new Bitmap[4];
        images[0] = Bitmap.getBitmapResource("scroll3.jpg");
        images[1] = Bitmap.getBitmapResource("scroll4.jpg");
        images[2] = Bitmap.getBitmapResource("scroll3.jpg");
        images[3] = Bitmap.getBitmapResource("scroll4.jpg");
        ScrollEntry[] entries = new ScrollEntry[4];

        for (int i = 0; i < entries.length; i++) { 
             entries[i] = new ScrollEntry(images[i], "", "");
        _pictureScrollField = new PictureScrollField(100,80){
        _pictureScrollField.setData(entries, 1);
        _pictureScrollField.setFocusListener(new FocusListener());
        _hfm = new HorizontalFieldManager();

        _containerheight = Display.getHeight() - _hfm.getPreferredHeight();
        _hfmbottomcontainer = new HorizontalFieldManager(){
        	 protected void sublayout(int maxWidth, int maxHeight) {
        	    super.sublayout(Display.getWidth(), _containerheight);
        	    setExtent(Display.getWidth(),_containerheight );

	private void addLeftPane(){
		_homeimage = GafaUtils.resizeBitmap(Bitmap.getBitmapResource("scroll4.jpg"),_containerwidth - 10, _containerheight -10);
		BitmapField homeimage = new BitmapField(_homeimage, BitmapField.FOCUSABLE ){
			protected boolean navigationClick(int status, int time ){
				Dialog.inform("Clicked on Home Image");	
				return false;
		_vfmleft = new VerticalFieldManager(){
			 protected void sublayout(int maxWidth, int maxHeight) {
				super.sublayout(_containerwidth, _containerheight);
	        	setExtent(_containerwidth, _containerheight);
	private void addRightPane(){

		LabelField pa = new LabelField ("Posting as", Field.FIELD_LEFT);
		_vfmright = new VerticalFieldManager(Field.USE_ALL_WIDTH){
			 protected void sublayout(int maxWidth, int maxHeight) {
				super.sublayout(Display.getWidth()/3, maxHeight);
	        	setExtent(Display.getWidth()/3, maxHeight);
	protected boolean navigationClick(int status, int time)
            Dialog.inform("You selected item " + _pictureScrollField.getCurrentImageIndex());
            return true;
        return super.navigationClick(status, time);
     * @see Screen#touchEvent()
    protected boolean touchEvent(TouchEvent message)
        if(message.getEvent() == TouchEvent.CLICK)
                Dialog.inform("You selected item " + _pictureScrollField.getCurrentImageIndex());
                return true;            
        return super.touchEvent(message);         
    public boolean onClose() {
	    return super.onClose();
    protected boolean onSavePrompt() {
    	return true;
    public void close() {
    class FocusListener implements FocusChangeListener {
    public void focusChanged(Field field, int eventType)
        if(field == _pictureScrollField && eventType == FOCUS_GAINED){
        else {


This is a class AScreen


package mypackage;

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

public class AScreen extends MainScreen{

	Screen prevScreen = null;
	public void openScreen(AScreen nextScreen) {
    	nextScreen.prevScreen = this;
	public void openPrevScreen() {
    	if (null != prevScreen)


Any help will be appreciated



Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Unusual java uncaught outofmemory error.

There is nothing immediately obvious in your code that looks completely wrong, but there are a few oddities which I will review later.


The thing that we don't see in your code is GafaUtils.  You could probably eliminate this as a potential source of problems by replacing that code with Bitmap scaleInto code. 


The most significant oddity in your code is the way you are handling close, or more accurately, onClose.  For a MainScreen, the standard close processing involves popping the current screen.  But you actually do that in onClose, which makes me wonder why you bother calling super.onClose().


Perhaps you cold remove the overriding of onClose, and just override close().  Do the same processing in there, and if there is not a previous screen, then call System.exit. 


You appear to be trying to create a stack of screens, where is screen a invokes Screen B, then closing Screen A will then cause the display of screen A.  The BB display stack works like that itself.  So if you push a screen on top of another screen, then when you pop that screen, the underlying screen is displayed.  So perhaps this code does not need to be present.


I think something in here is the most likely cause of the problem you see.  But I'm not sure why it should be.  I'd be looking for some kind of recursion, but I can't see it immediately.  And if the application actually exits, then there is nothing in the code we see here that should be left behind and use up memory.  . 


You should be aware that MainScreen close() method will in fact close the Application if you pop the last screen off. 


Finally in our discussion about MainScreen, you should review this KB article:


You seem to be trying to partition your screen in the same way the MainScreen already provides with its title, banner and status areas. 


Moving on to other things....


You use this or similar code in your sublayout for a number of Managers:

          protected void sublayout(int maxWidth, int maxHeight) {
            super.sublayout(Display.getWidth(), _containerheight);
            setExtent(Display.getWidth(),_containerheight );

I have two issues here:

a) I recommend that if you think you need to set the Extent for a Manager, then you should in fact be positioning the Fields yourself.  What you have dne above is told the Manager that the biggest area it can use is

Display.getWidth(), _containerheight

and then, regardless of how much space it actually wants to use, you tell it that it will use

Display.getWidth(), _containerheight

This can cause issues with Managers, because they assume that if you allowed them to layout the Fields, then they know how much space they take up. 

b) Secondly you should make sure that you use, at most, the space that you are allowed.  So the parameters you pass in toe super.layout should be the minimum of the values you want and the values that have been passed in. 


I can't see the focus change listener being used?


Anyway, if you have a play round and still can't get this working, please post some code that we actually test with and tell us the OS and Simulator you are testing on.  Then we can see if we see the problem too.