Welcome!

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

Reply
Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

out of memory error

http://s11.postimage.org/pxswu35g1/Untitled.png

 

I get out of memory error, and that page?

 

What am doing is I have  a list... but ma not using listfield, instead done a custom one ..

its very big 488 lines...so i got 488 verticalfieldmanagers in the page...with buttons on each line...

 

when i click on a button to go to new page  i get that error...out of memory error...

If i downsize data to 20...i dont get it...

 

 

here is page code..

 

final VerticalFieldManager Content= new VerticalFieldManager(Manager.USE_ALL_WIDTH|Manager.VERTICAL_SCROLL);
		Content.setMargin(5,0,0,0);
		int mas  = 0 ;
		while(mas < GlobalValue.avD.size())
		{
			Song temp = (Song) GlobalValue.avD.elementAt(mas);
			Content.add(new A_AddSingers2(temp.albumId, temp.albumName, false,temp.albumCover,temp));
			
			mas++;
		}
		add(Content);

 

 

 

 

And the a_addsingers2 class

 

 

package mypackage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;

import net.rim.device.api.io.MalformedURIException;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.system.GIFEncodedImage;
import net.rim.device.api.system.PNGEncodedImage;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
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.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.component.Status;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class A_AddSingers2 extends VerticalFieldManager implements FocusChangeListener {
	int BorderColor = 0x60347A;
	//	int textColor = 0x60347A;
	int textColor = Color.BLACK;
	public HorizontalFieldManager SubContainer;
	public HorizontalFieldManager jawwStamp;
	public VerticalFieldManager SubContainer32;
	JustifiedHorizontalFieldManager Container;
	public  HorizontalFieldManager AddRemove;
	LabelField L_ArtistName;
	BitmapField B_Image,B_Add,B_Remove,B_Download,bjaww,B_Play,B_Stop;
	String ArtistTitle;

	int ArtistId;
	boolean Checked;
	byte[] Cover;
	boolean _focused = false;
	Song temp= new Song();
	public A_AddSingers2(int pArtistId,String pArtistTitle,boolean pChecked,byte[] pCover,Song pTemp) {
		Cover=pCover;
		temp =pTemp;
		ArtistId=pArtistId;
		ArtistTitle=pArtistTitle;
		Checked=pChecked;
		if(pCover != null)
		{
			B_Image =  new BitmapField(MAMClient.sizeImage(EncodedImage.createEncodedImage(pCover, 0, pCover.length),100,100).getBitmap()){  
				public void paint(Graphics graphics) {
					super.paint(graphics);
					graphics.setColor(BorderColor);
					graphics.drawRect(0,0,getWidth(),getHeight());
					graphics.drawRect(1,1, getWidth(), getHeight());
				}};
		}
		SubContainer= new HorizontalFieldManager(Manager.FIELD_VCENTER);
		SubContainer32= new VerticalFieldManager(Manager.FIELD_VCENTER)
		{
			protected boolean navigationClick(int status, int time)
			{ 

				//UiApplication.getUiApplication().pushScreen(new SongAlbum(temp.albumId,tb,logIn,logOut,Register));
				//	GlobalValue.tb.displayTab10(temp);
				return true;
			}
			protected void drawFocus(Graphics g,boolean yes) {      
				invalidate();
				super.drawFocus(g, false);}

			protected void paint(Graphics graphics) {
				//graphics.setColor(Color.AZURE);
				super.paint(graphics);}

			protected void onFocus(int direction) {
				try{

					//	textColor =Color.AZURE;

					invalidate();
					super.onFocus(direction);	//cheat HFM: set direction to forward
				}catch(Exception ex){
					ex.getMessage();}}

			protected void onUnfocus() {

				invalidate();
				super.onUnfocus();
			}
			public int getPreferredWidth() { 
				return Display.getWidth()-(100) ; 
			}
		};
		AddRemove= new HorizontalFieldManager(Manager.FIELD_VCENTER);
		L_ArtistName= new LabelField( temp.getSongNam(),Manager.NON_FOCUSABLE|DrawStyle.ELLIPSIS|Manager.FIELD_VCENTER){
			protected boolean navigationClick(int status, int time)
			{ 

				//UiApplication.getUiApplication().pushScreen(new SongAlbum(temp.albumId,tb,logIn,logOut,Register));
				return true;
			}
			protected void drawFocus(Graphics g,boolean yes) {      
				invalidate();
				super.drawFocus(g, false);}

			protected void paint(Graphics graphics) {
				graphics.setColor(Color.GREEN);
				super.paint(graphics);}

			protected void onFocus(int direction) {
				try{

					//	textColor =Color.AZURE;

					invalidate();
					super.onFocus(direction);	//cheat HFM: set direction to forward
				}catch(Exception ex){
					ex.getMessage();}}

			protected void onUnfocus() {

				invalidate();
				super.onUnfocus();
			}
			public int getPreferredWidth() { 
				return Display.getWidth()-(300) ; 
			}
			protected void layout(int maxWidth,int maxHeight) {
				super.layout(getPreferredWidth(),maxHeight);
				setExtent(getPreferredWidth(), getHeight());
			}};
			LabelField L2_ArtistName= new LabelField( temp.getSingerNam(),Manager.FOCUSABLE|DrawStyle.ELLIPSIS|Manager.FIELD_VCENTER)
			{
				protected boolean navigationClick(int status, int time)
				{ 

					//	UiApplication.getUiApplication().pushScreen(new SongAlbum(temp.albumId,tb,logIn,logOut,Register));
					return true;
				}
				protected void drawFocus(Graphics g,boolean yes) {      
					invalidate();
					super.drawFocus(g, false);}

				protected void paint(Graphics graphics) {
					//graphics.setColor(Color.AZURE);
					super.paint(graphics);}


				public int getPreferredWidth() { 
					return Display.getWidth()-(100) ; 
				}
				protected void layout(int maxWidth,int maxHeight) {
					super.layout(getPreferredWidth(),maxHeight);
					setExtent(getPreferredWidth(), getHeight());
				}};

				Font font1 = getFont().derive(Font.BOLD, 32);
				L_ArtistName.setFont(font1);
				B_Add= new BitmapField( Bitmap.getBitmapResource(GlobalValue.getReg5()),BitmapField.FOCUSABLE|Manager.FIELD_VCENTER){
					protected boolean navigationClick(int status, int time)
					{ 
						if(GlobalValue.getUser().getId() != -1)
						{
							GlobalValue.addToPlaylist =3;
							GlobalValue.playL = new Vector();
							GlobalValue.playL.addElement(temp);
							//UiApplication.getUiApplication().pushScreen(new Log(0));
							//UiApplication.getUiApplication().popScreen();

							// Dialog.inform("PlayList Page");
							//UiApplication.getUiApplication().pushScreen(new PlayList(-1));
							HorizontalFieldManager popHF = new HorizontalFieldManager();
							// popHF.add(new LabelField("Pls wait..."));
							final PopupScreen waitScreen = new PopupScreen(popHF);
							Thread threadToRun = new Thread() 
							{
								public void run() 
								{

									synchronized (UiApplication.getEventLock()) 
									{
										HorizontalFieldManager loadingManager = new  HorizontalFieldManager(Manager.FIELD_HCENTER);
										final GIFEncodedImage ourAnimation = (GIFEncodedImage) GIFEncodedImage.getEncodedImageResource("gif-loading.agif");



										AnimatedGIFField  _ourAnimation = new AnimatedGIFField(ourAnimation, Field.FIELD_HCENTER);

										_ourAnimation.setMargin(4, 10, 0, 0);
										loadingManager.setMargin(0, 0, 7, 0);
										loadingManager.add(_ourAnimation);
										waitScreen.add(loadingManager);

										UiApplication.getUiApplication().pushScreen(waitScreen);
									}
									synchronized (UiApplication.getEventLock()) 
									{
										SQLManager pos = new SQLManager();
										if(GlobalValue.downList.size() == 0)
											pos.getPlayListTrack(GlobalValue.downList);
										UiApplication.getUiApplication().popScreen();
										UiApplication.getUiApplication().pushScreen(new myLib(0));

									}
									return;
								}    
							};
							threadToRun.start();

						}
						else
						{
							SQLManager pa = new SQLManager();
							pa.getUser();
							// UiApplication.getUiApplication().pushScreen(new SignIn()); 
							UiApplication.getUiApplication().pushScreen(new Log(0));
						}
						return true;
					}


				};

				B_Play= new BitmapField( Bitmap.getBitmapResource(GlobalValue.getReg11()),BitmapField.FOCUSABLE|Manager.FIELD_VCENTER){
					protected boolean navigationClick(int status, int time)
					{ 
						try
						{
							GlobalValue.RadioPlayer.removePlayerListener(GlobalValue.pm);
						}
						catch(Exception e)
						{

						}
						AddRemove.replace(B_Play, B_Stop);
						GlobalValue.songP = 0;
						GlobalValue.songT = new Vector();
						try
						{
							GlobalValue.hMan.replace( GlobalValue.B_Stop,  GlobalValue.B_Play);
						}
						catch(Exception e)
						{

						}
						GlobalValue.B_Play = B_Play;
						GlobalValue.B_Stop = B_Stop;
						GlobalValue.hMan = AddRemove;

						if(temp.getTrailer() != null)
						{
							try
							{

								if(GlobalValue.RadioPlayer != null)
								{
									GlobalValue.RadioPlayer.stop();
									GlobalValue.RadioPlayer.close();
									GlobalValue.RadioPlayer = null;
								}

								HttpConnection google = (HttpConnection)javax.microedition.io.Connector.open(temp.getTrailer()+";interface=wifi;deviceside=true");

								InputStream is = google.openInputStream();
								GlobalValue.RadioPlayer = javax.microedition.media.Manager.createPlayer(is, "audio/mpeg");
								GlobalValue.RadioPlayer.realize();
								GlobalValue.RadioPlayer.prefetch();
								GlobalValue.RadioPlayer.start();        
								GlobalValue.RadioPlayer.addPlayerListener(GlobalValue.pm);


							}
							catch(Exception e){}
						}
						// GlobalValue.tb.displayTab();
						return true;
					}


				};
				B_Stop= new BitmapField( Bitmap.getBitmapResource(GlobalValue.getReg12()),BitmapField.FOCUSABLE|Manager.FIELD_VCENTER){
					protected boolean navigationClick(int status, int time)
					{ 
						try
						{

							AddRemove.replace(B_Stop, B_Play);
							GlobalValue.hMan = null;
							GlobalValue.B_Play = null;
							GlobalValue.B_Stop = null;

						}
						catch(Exception e)
						{

						}
						try
						{

							if(GlobalValue.RadioPlayer != null)
							{
								try {
									GlobalValue.RadioPlayer.removePlayerListener(GlobalValue.pm);
								}
								catch(Exception e)
								{

								}
								try {
									GlobalValue.RadioPlayer.stop();
								} catch (MediaException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								GlobalValue.RadioPlayer.close();
								GlobalValue.RadioPlayer = null;
							}

						}
						catch(Exception e)
						{

						}
						return true;
					}


				};
				//		B_Add.setFocusListener(this);			
				B_Remove= new BitmapField( Bitmap.getBitmapResource(GlobalValue.getReg5()),BitmapField.FOCUSABLE|Manager.FIELD_VCENTER){
					protected boolean navigationClick(int status, int time){ 
						String s = (ArtistId+"-"+0);
						//	 MAMClient.SubmitArtistSubsc_Android(GV.UserID ,s);
						AddRemove.replace(B_Remove, B_Add);

						return true;}
					protected void drawFocus(Graphics g,boolean yes) {      
						invalidate();
						super.drawFocus(g, false);}

					protected void onFocus(int direction) {    
						this.setBitmap(Bitmap.getBitmapResource(GlobalValue.getReg5()));  
						invalidate();
						super.onFocus(direction);}   

					protected void onUnfocus() {     
						this.setBitmap(Bitmap.getBitmapResource(GlobalValue.getReg5()));  
						invalidate();
						super.onUnfocus();}};
						B_Remove.setFocusListener(this);	
						B_Download= new BitmapField( Bitmap.getBitmapResource(GlobalValue.getReg6()),BitmapField.FOCUSABLE|Manager.FIELD_VCENTER){
							protected boolean navigationClick(int status, int time)
							{ 
								if(GlobalValue.getUser().getId() != -1)
								{
									final String resulto = MAMClient.getSongEncPath(0, temp.getSongId(),1);
									VideoCLip m = new VideoCLip();
									m.setId(-1);
									GaugeField	ProgressBar= new GaugeField(null, 0, 100, 0, GaugeField.NO_TEXT);
									try
									{
										FileConnection fileConnection3 = (FileConnection)Connector.open("file:///SDCard/downloads/"+temp.getSongName());    

										// If the file is blank, copy the pre-defined database from this
										// module to the SDCard.
										if(!fileConnection3.exists())
										{       
											MAMDownloadService  downloadService =  GlobalValue.downloadService;
											downloadService.startDownload(temp, m, 1,temp.getSongName(),temp.getSongName(),temp.getSongId(),temp.getSingerId(),
													temp.getAlbumId(),"MEDIA","Down", resulto, ProgressBar,temp.getByteSize(),resulto);
										}
									}
									catch(Exception e)
									{

									}

								}
								else
								{
									SQLManager pa = new SQLManager();
									pa.getUser();
									// UiApplication.getUiApplication().pushScreen(new SignIn()); 
									UiApplication.getUiApplication().pushScreen(new Log(0));
								}


								return true;}


						};
						//B_Download.setFocusListener(this);
						if(Checked==true){
							AddRemove.add(B_Remove);
						}
						else{
							B_Play.setMargin(0,10,0,10);
							AddRemove.add(B_Play);
							B_Add.setMargin(0, 10, 0, 10);
							AddRemove.add(B_Add);
						}
						AddRemove.add(B_Download);
						jawwStamp= new HorizontalFieldManager(Manager.FIELD_LEFT);
						jawwStamp.add(L_ArtistName);
						if(temp.hasStamp == 1)
						{
							/*Bitmap neo = Bitmap.getBitmapResource("Jaww_stamp03.png");
			PNGEncodedImage encodedImage =  PNGEncodedImage.encode(neo);
			byte[] neo1 = encodedImage.getData();;
			Bitmap neo2 = MAMClient.sizeImage(EncodedImage.createEncodedImage(neo1, 0, neo1.length),40,40).getBitmap();
			 bjaww= new BitmapField(neo2){
				 protected boolean navigationClick(int status, int time){ 

				 return true;}
			 };
			 jawwStamp.add(bjaww);*/

						}
						SubContainer32.add(jawwStamp);
						//SubContainer32.add(L_ArtistName);
						SubContainer32.add(L2_ArtistName);
						if(pCover != null)
							SubContainer.add(B_Image);
						SubContainer.add(SubContainer32);
						Container = new JustifiedHorizontalFieldManager(SubContainer,AddRemove,false, USE_ALL_WIDTH |Manager.FIELD_HCENTER);
						if(pCover != null)
							B_Image.setMargin(0, 5, 0,0);//top,right,bottom,left
						Container.setMargin(0,5,0,5);//top,right,bottom,left
						add(Container);
						add(new SeparatorField(SeparatorField.LINE_HORIZONTAL){
							protected void paint(Graphics g){									   
								// g.setBackgroundColor(0x60347A);		
								g.setBackgroundColor(Color.GREEN);	
								g.clear();									
								super.paint(g);}});}

	protected void paint(Graphics graphics) {
		graphics.setColor(textColor);
		super.paint(graphics);}	

	protected void paintBackground(Graphics g) {
		if( _focused==true){
			// g.setColor(0x60347A);
			g.setColor(Color.BLUE);
			g.fillRect(0, 0, Display.getWidth(), this.getHeight());}}

	protected void onFocus(int direction) {
		textColor = 0x858686;
		_focused= true;
		invalidate();
		super.onFocus(direction);	//cheat HFM: set direction to forward
	}
	protected void onUnfocus() {
		//textColor =0x60347A;
		textColor = Color.BLACK;
		_focused=false;
		invalidate();
		super.onUnfocus();
	}
	public void focusChanged(Field field, int eventType) 
	{
		if (field == B_Add||field == B_Remove){
			switch (eventType){
			case FOCUS_GAINED:
				textColor = 0x858686;
				BorderColor = 0x858686;
				_focused= true;
				invalidate();
				break;
			case FOCUS_LOST:
				//textColor =0x60347A;
				textColor = Color.BLACK;
				BorderColor =0x60347A;
				_focused=false;
				invalidate();
				break;

			}
		}
	}




}

 

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: out of memory error

GlobalValue.avD.size() is 488
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: out of memory error

In my opinion, this is really too much code to expect anyone to work through.  And to be honest, it is difficult code too.  Can you structure this in some way to make it simpler to read?

 

Here are some random comments:

 

In my opinion displaying 488 rows on a page is too many, I suggest that you 'page' them, say 25 per page.  Do you look at 488 Google responses all at once?  This will cut cown the space requirement per page significantly as well as giving a better user expericnce (in my opinion anyway). 

 

this is dubious code:

   protected void drawFocus(Graphics g,boolean yes) {     
invalidate();
super.drawFocus(g, false);}

If you just want to leave the Field in an unfocused state, then just return. 

 

This code may screw things up:

   protected void layout(int maxWidth,int maxHeight) {
super.layout(getPreferredWidth(),maxHeight);
setExtent(getPreferredWidth(), getHeight());
}};

If you are going t bother to let a Manager layout itself, you should respect the width that it uses.  If you want it to use all the Width you give it, using USE_ALL_WIDTH as a stule. 

 

I recommend that you avoind

synchronized (UiApplication.getEventLock()) {

}

blocks unless you know what you are doing.  Instead using

UiApplication.getUiApplication.invokelater(...);

 

Don't run background processing like SQL data of network connections on the event thread or in syncrhonised blocks:

For example:

 SQLManager pos = new SQLManager();
if(GlobalValue.downList.size() == 0)
pos.getPlayListTrack(GlobalValue.downList);

and:

ttpConnection google = (HttpConnection)javax.microedition.io.Connector.open(temp.getTrailer()+";interface=wifi;deviceside=true");

 Processing of a navigationClick or other user interaction is done on the Event Thread

 

One more thing

 

You say that the problem occurs when you 'click'.  I presume therefore thisis when you are trying to display your second screen.  So the problem might not be in this code at all, it might be in the second screen.  Can you try just starting the second screen without this screen first?.

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: out of memory error

if I do it 25 like usay, on click..and i got ot new page with no problems.

I am pretty sure its from the 488 thing, because i tested on small samples..i get no errors..

 

but on 488...on going to new page i am hit with out of memory error..

 

 

I can do it 25 list, and with load more button..on click of load it brings more data.however, how different it is? if it clicsk 50 times...it will be 488 data at end.

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

Re: out of memory error

Sorry, now that I re-read your description, you did point out that 20 was fine, so some of my comments were not useful.

 

Yes if you click more 50 times you will have downloaded the same number, but the screen will only display one page at a time, not all.  So things like the memory requirements for the Bitmaps you have displayed will be significantly less when you are only displaying 25 and not 488.  That is what I mean. 

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: out of memory error

Here is what i did, I only bring 25.

And yes it works, on click of button it goes to new page normally.

 

However, I added load more button..and when its clicked..it add more rows.. I clicked it like 20 times..

I ended with 450 rows...and then when I clicked on the button it went to the same error out of memory error!

Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: out of memory error

don't add the new rows, replace them.
you can show the user that he is on page 12/25, for example, and let him switch back and forth.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: out of memory error

[ Edited ]

what you suggest is logical, and how I intend to do things.

But I have **bleep** boss, who refuses so and has zero clue on development.  At first I WANTED to use listfield , no buttons in the list..but functionality in context menu. I was forced by my boss to break standards and do  custom list with buttons inside..

i ended up with that class, a_addsingers2...i end up with 450 buttons in  the whole page

 

now I want to do it like u suggest,m page flipping...but  the **bleep** insists on my head with load more functionality..and on same page.

 

 

Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: out of memory error

you will either have an application that gets out of memory errors, or your boss will have to adjust his wishes to the limitations of a mobile platform.
you can draw a button in your listfield, it won't be focusable of course, but maybe the look is enough Smiley Happy
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter