Too many thread exceptions when using a custom list box in Blackberry

I am developing a custom list box using the Table Row Manager in blackberry.it displays a list of items coming from a web service. When I click on an item, it goes to another screen. But I get too many thread exceptions. After a while, I get too many thread exceptions when I click on an element. Then I check when the thread is created using debug.Then I found a mapping of each element creating a separate thread. How can I solve this problem, please help here, this is my custom list box class

class LabelListField extends ListField implements ListFieldCallback { private Vector mValues; private Vector mRows; DynamicImages images; int[] intColor=new int[500]; int i=0; int j=0,position; static int value1=0; String key; String[] col; public LabelListField(Vector values,int p,String key) { super(0); setRowHeight(70); setCallback(this); position=p; mValues = values; this.key=key; fillListWithValues(values); images=new DynamicImages(); scheduleInvalidate(); } private void scheduleInvalidate() { Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { invalidate(); } }, 0, 100); } private void fillListWithValues(Vector values) { mRows = new Vector(); for (;i< values.size();i++) { TableRowManager row = new TableRowManager(); String value = (String) values.elementAt(i); ListLabel valueLabel = new ListLabel(this,i,value); if(Display.getWidth()==480) { valueLabel.setFont(Utility.getBigFont(16)); } else { valueLabel.setFont(Utility.getBigFont(12)); } row.add(valueLabel); mRows.addElement(row); } setSize(mRows.size()); } private class TableRowManager extends Manager { public TableRowManager() { super(0); } public void drawRow(Graphics g, int x, int y, int width, int height) { layout(width, height); setPosition(x, y); g.pushRegion(getExtent()); paintChild(g, getField(0)); Bitmap line=Bitmap.getBitmapResource(images.lightline); g.drawBitmap(0,0,line.getWidth(),line.getHeight(),line,0,0); g.popContext(); } protected void sublayout(int width, int height) { int fontHeight = Font.getDefault().getHeight(); int preferredWidth = getPreferredWidth(); Field field = getField(0); layoutChild(field, preferredWidth,fontHeight + 1); if(((ListLabel)field).getText().length()>110) { setPositionChild(field, 5,10); } else if(((ListLabel)field).getText().length()>55) { setPositionChild(field,5,20); } else if(((ListLabel)field).getText().length()<55) { setPositionChild(field,5,30); } //field = getField(1); //layoutChild(field,18,24); //setPositionChild(field,250,30); setExtent(getPreferredWidth(), getPreferredHeight()); } public int getPreferredWidth() { return Display.getWidth(); } public int getPreferredHeight() { return getRowHeight(); } } public void drawListRow(ListField listField, Graphics g, int index, int y, int width) { String val=HomeScreenIcons.colorstable.get(key).toString(); col=StringToken.split(val,"||"); if (g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)) { LabelListField list = (LabelListField) listField; g.setColor(0xC0C0C0); g.fillRect(0,y+0,480,list.getRowHeight()); if(col[index].equals("1")) { g.setColor(0x004D7B); } else { g.setColor(Color.GRAY); } TableRowManager rowManager = (TableRowManager) list.mRows .elementAt(index); rowManager.drawRow(g,0,y,width,list.getRowHeight()); } else { if(col[index].equals("1")) { g.setColor(0x004D7B); } else { g.setColor(Color.GRAY); } LabelListField list = (LabelListField) listField; TableRowManager rowManager = (TableRowManager) list.mRows .elementAt(index); rowManager.drawRow(g,0,y,width,list.getRowHeight()); } } public Object get(ListField list, int index) { return mValues.elementAt(index); } public int indexOfList(ListField list, String prefix, int start) { for (int x = start; x < mValues.size(); ++x) { String value = (String) mValues.elementAt(x); if (value.startsWith(prefix)) { return x; } } return -1; } public int getPreferredWidth(ListField list) { return Display.getWidth(); } class ListLabel extends LabelField { int mIndex = -1; String text; int[] color=new int[500]; public ListLabel(LabelListField list, int index, String text) { super(text); this.text=text; mIndex = index; System.out.println("position is"+position); } public int getPreferredWidth() { return Display.getWidth()-80; } protected void layout(int maxWidth,int maxHeight) { super.layout(getPreferredWidth(),maxHeight); setExtent(getPreferredWidth(), getHeight()); } protected boolean navigationClick(int status, int time) { fieldChangeNotify(0); return true; } protected void fieldChangeNotify(int context){ if(context == 0){ try { this.getChangeListener().fieldChanged(this, context); } catch (Exception e){} } } } protected boolean trackwheelClick(int status, int time) { return true; } } 
0
blackberry
source share
2 answers

useless call to scheduleInvalidate method. When you delete scheduleInvalidate() it works fine

+1
source share

koti, to add to your own answer - one of my BB applications also needs to start threads for each line of the list.

BlackBerry can have only a few threads. I recommend creating a task workflow stream and setting tasks on it. Thus, you can have many background tasks, but they are in only one thread.

Another option in your case would be to have only one Timer thread containing a list of strings. This 1 thread can then invalidate each row at a specific time. This is better than having a new timer entry for each row.

+1
source share

All Articles