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: 264
Registered: ‎06-04-2009
My Device: Not Specified

Re: Too many threads error

I nsuch case then there is no other thread . Is it because  i am not popping the screens this error is coming ??

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Too many threads error

AFAIK, a non-modal display does not create a thread. However, if you pushModal() this *DOES* create a thread for each screen pushed.

 

You can certainly identify where your threads are coming from if you open the Threads view in the debugger.

 

 

 

Developer
Posts: 264
Registered: ‎06-04-2009
My Device: Not Specified

Re: Too many threads error

Hi

 I checked the thread view in JDE but i cudnt find the number of threads.

Even after popping the screen each time the exception is coming

I am using activeRichtextfield. Is it having any relation to the thread ??

other than that I am not able find from where the threads come

 

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Too many threads error

Developer
Posts: 264
Registered: ‎06-04-2009
My Device: Not Specified

Re: Too many threads error

No

I am not using Timer at all

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Too many threads error

Well, you are doing something to create threads. You say that you opened the Threads view in the debugger, yet you could not see your threads?

 

Are you creating a Player (also creates thread)?

 

Maybe you should tell us what your program is doing?

 

 

 

Developer
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: Too many threads error

It may be worth setting a break point. Sometimes, at least in Eclipse, the Thread view only shows all threads once the VM has been paused by or hit a break point. 

Highlighted
Developer
Posts: 264
Registered: ‎06-04-2009
My Device: Not Specified

Re: Too many threads error

Sorry all

 

I got the issue. My application has a headerbar which shows device battery status time etc. to update that i was using timer task and the header bar is a custom field. in all screen i was creating instance of it so new threads get created. Now i have one more issue

I am not able to make single object of headerbar and setTitle for each screen. Its giving Uncaught exception: Field added to a manager while it is already parented

 

The code is as follows is there any way i can update this title field

 

import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
//
public class HeaderBar extends Field implements DrawStyle{
private Font headerFont = SmallFont();
private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
private String timeString;
private String dateString;
private String title;
private boolean showSignal;
private boolean showBattery;
private boolean showDate;
private boolean showTime;
private boolean showTitle;
private int fieldWidth;
private int fieldHeight;
private int fontColour;
private int backgroundColour;
private int batteryBackground;
private Timer headerTimer = new Timer();
private TimerTask headerTask;
//
public HeaderBar(String _title) {
super(Field.NON_FOCUSABLE);
title = _title;
showSignal = true;
showBattery = true;
showDate = true;
showTime = true;
showTitle = true;
fieldHeight = 12;
fieldWidth = Graphics.getScreenWidth();
fontColour = 0x888888;
backgroundColour = 0;
batteryBackground = 0x999999;
//
headerTask = new TimerTask() {
public void run() {
invalidate();
}
};
//
headerTimer.scheduleAtFixedRate(headerTask, 1000, 1000);
}
//
public void setTitle(String _title){
title = _title;
invalidate();
}
//
public void setFontColour(int _fontColour){
fontColour = _fontColour;
invalidate();
}
//
public void setBatteryBackground(int _batteryBackground){
batteryBackground = _batteryBackground;
invalidate();
}
//
public void setBackgroundColour(int _backgroundColour){
backgroundColour = _backgroundColour;
invalidate();
}
//
public void showSignal(boolean bool){
showSignal = bool;
invalidate();
}
//
public void showBattery(boolean bool){
showBattery = bool;
invalidate();
}
//
public void showDate(boolean bool){
showDate = bool;
invalidate();
}
//
public void showTime(boolean bool){
showTime = bool;
invalidate();
}
//
public void showTitle(boolean bool){
showTitle = bool;
invalidate();
}
//
protected void layout(int width, int height) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
//
public int getPreferredWidth() {
return fieldWidth;
}
//
public int getPreferredHeight() {
return fieldHeight;
}
//
protected void paint(Graphics graphics) {
graphics.setFont(headerFont);
int graphicsDiff = 0;
//
if(backgroundColour != 0){
graphics.setColor(backgroundColour);
graphics.fillRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
}
//
if(showSignal){
graphicsDiff = graphicsDiff + 28;
graphics.setColor(0x999999);
graphics.fillRect(this.getPreferredWidth() - 26, 8, 6, 4);
graphics.fillRect(this.getPreferredWidth() - 21, 6, 6, 6);
graphics.fillRect(this.getPreferredWidth() - 16, 4, 6, 8 );
graphics.fillRect(this.getPreferredWidth() - 11, 2, 6, 10);
graphics.fillRect(this.getPreferredWidth() - 6, 0, 6, 12);
int signalLevel = RadioInfo.getSignalLevel();
if(signalLevel > -77){
//5 bands
graphics.setColor(0xEFEFEF);
graphics.fillRect(this.getPreferredWidth() - 25,9,4,2);
graphics.fillRect(this.getPreferredWidth() - 20,7,4,4);
graphics.fillRect(this.getPreferredWidth() - 15,5,4,6);
graphics.fillRect(this.getPreferredWidth() - 10,3,4,8);
graphics.fillRect(this.getPreferredWidth() - 5, 1, 4, 10);
}else if(signalLevel > -86){
//4 bands
graphics.setColor(0xEFEFEF);
graphics.fillRect(this.getPreferredWidth() - 25,9,4,2);
graphics.fillRect(this.getPreferredWidth() - 20,7,4,4);
graphics.fillRect(this.getPreferredWidth() - 15,5,4,6);
graphics.fillRect(this.getPreferredWidth() - 10,3,4,8);
}else if(signalLevel > -92){
//3 bands
graphics.setColor(0xEFEFEF);
graphics.fillRect(this.getPreferredWidth() - 25,9,4,2);
graphics.fillRect(this.getPreferredWidth() - 20,7,4,4);
graphics.fillRect(this.getPreferredWidth() - 15,5,4,6);
}else if(signalLevel > -101){
//2 bands
graphics.setColor(0xEFEFEF);
graphics.fillRect(this.getPreferredWidth() - 25,9,4,2);
graphics.fillRect(this.getPreferredWidth() - 20,7,4,4);
}else if(signalLevel > -120){
//1 band
graphics.setColor(0xEFEFEF);
graphics.fillRect(this.getPreferredWidth() - 25,9,4,2);
}
}
if(showBattery){
//
int batteryLevel = DeviceInfo.getBatteryLevel();
graphics.setColor(batteryBackground);
graphics.fillRect(this.getPreferredWidth()-23-graphicsDiff,2,20,8);
graphics.fillRect(this.getPreferredWidth()-3-graphicsDiff,4,1,4);
if(batteryLevel > 75){
graphics.setColor(0x28f300);
}else if(batteryLevel > 50){
graphics.setColor(0x91dc00);
}else if(batteryLevel > 25){
graphics.setColor(0xefec00);
}else{
graphics.setColor(0xff2200);
}
double powerLong = ((18.00/100)*batteryLevel);
int power = (int)powerLong;
graphics.fillRect(this.getPreferredWidth()-22-graphicsDiff,3,power,6);
graphicsDiff = graphicsDiff + 24;
}
//
graphics.setColor(fontColour);
//
if(showTime){
timeString = " " + timeFormat.formatLocal(System.currentTimeMillis()) + " ";
}else{
timeString = "";
}
//
if(showDate){
dateString = " " + dateFormat.formatLocal(System.currentTimeMillis()) + " ";
}else{
dateString = "";
}
//
graphics.drawText(dateString + timeString, this.getPreferredWidth()-headerFont.getAdvance(dateString + timeString)-graphicsDiff, 1);
//
if(showTitle){
int limit = 0;
if(showSignal)limit = limit + 28;
if(showBattery)limit = limit + 25;
if(showTime)limit = limit + headerFont.getAdvance(timeString);
if(showDate)limit = limit + headerFont.getAdvance(dateString);
if(headerFont.getAdvance(title) > this.getPreferredWidth() - limit){
int elippsy = headerFont.getAdvance("...");
int availableWidth = this.getPreferredWidth() - limit - elippsy;
String _txt = "";
String shorterTitle = "";
for(int i = 1 ; i < title.length(); i++){
_txt = title.substring(0, i);
if(headerFont.getAdvance(_txt) < availableWidth){
shorterTitle = _txt;
}
}
title = shorterTitle + "...";
}
graphics.drawText(title, 1, 0);
}
}
//
public static Font SmallFont(){
try {
FontFamily theFam = FontFamily.forName("SYSTEM");
return theFam.getFont(net.rim.device.api.ui.Font.PLAIN, 10);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return null;
}
} 

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Too many threads error

A field can only belong to one manager.

 

You'll have to figure a different way to handle the status bar, or simply refrain from creating screens until they are needed.

 

 

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Too many threads error

[ Edited ]

You do not need to create a new Timer instance everytime you want to schedule a task. One can handle what you need (make a static instance somewhere). This will get you down to one Timer Thread.