而在黑莓使用自定义列表场线程太多例外 [英] Too Many threads exception while using custom list field in Blackberry

查看:340
本文介绍了而在黑莓使用自定义列表场线程太多例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用表行经理blackberry.it显示从网络未来的产品清单service.When我点击项目自定义列表场它去其他screen.But我得到太多的线程exception.after一段时间我我得到太多的线程当我点击item.Then当线程使用debug.Then创建我检查例外,我发现显示的每个项目就创建单独的thread.How我能解决这个问题,请帮助这里是我的自定义列表场类

 类LabelListField扩展ListField实现ListFieldCallback
{
    私人矢量mValues​​;
    私人矢量mRows;
    DynamicImages图像;
    INT [] = intColor新INT [500];
    INT I = 0;
    INT J = 0,位置;
    静态INT值1 = 0;
    字符串键;
    的String []关口;
    公共LabelListField(矢量值,INT P,字符串键){
        超级(0);
        setRowHeight(70);
        setCallback(本);
        位置= P;
        mValues​​ =值;
        this.key =键;
        fillListWithValues​​(值);
        照片=新DynamicImages();
        scheduleInvalidate();
    }    私人无效scheduleInvalidate(){
        定时器定时器=新的Timer();
        timer.scheduleAtFixedRate(新的TimerTask(){
            公共无效的run(){
                无效();
            }
        },0,100);
    }    私人无效fillListWithValues​​(矢量值){
        mRows =新的向量();
        对于(; I< values​​.size();我++){           TableRowManager行=新TableRowManager();
           字符串值=(字符串)values​​.elementAt(I)
            ListLabel valueLabel =新ListLabel(这个,我,值);
            如果(Display.getWidth()== 480)
            {
            valueLabel.setFont(Utility.getBigFont(16));
            }
            其他
            {
            valueLabel.setFont(Utility.getBigFont(12));
            }
            row.add(valueLabel);            mRows.addElement(行);
        }        的setSize(mRows.size());
    }    私有类TableRowManager扩展管理器{
        公共TableRowManager(){
            超级(0);
        }        公共无效drawRow(图形克,INT X,INT Y,
            INT宽度,高度INT){
            布局(宽,高);
            的setPosition(X,Y);
            g.pushRegion(getExtent());
            paintChild(G,getfield命令(0));
            位图线= Bitmap.getBitma $ P $的PSource(images.lightline);
            g.drawBitmap(0,0,line.getWidth(),line.getHeight(),线,0,0);
            g.popContext();
        }
    保护无效sublayout(INT宽度,高度INT){
            INT fontHeight = Font.getDefault()的getHeight()。
            INT preferredWidth = GET preferredWidth();
            场场= getfield命令(0);
            layoutChild(场,preferredWidth,fontHeight + 1);
            如果(((ListLabel)字段).getText()长度()温度保持在110)
            {
            setPositionChild(场,5,10);
            }
            否则如果(((ListLabel)字段).getText()长度()方式> 55)
            {
             setPositionChild(场,5,20);
            }
            否则,如果(((ListLabel)字段).getText()长()< 55)
            {
             setPositionChild(场,5,30);
            }
            //字段= getfield命令(1);
            // layoutChild(场,18,24);
            // setPositionChild(场,250,30);
            setExtent(GET preferredWidth(),获得preferredHeight());
        }        公众诠释的get preferredWidth(){
            返回Display.getWidth();
        }        公众诠释的get preferredHeight(){
            返回getRowHeight();
        }
    }    公共无效drawListRow(ListField listField,图形克,
            INT指数,诠释Y,int width)将{
        字符串VAL = HomeScreenIcons.colorstable.get(密钥)的ToString();
        COL = StringToken.split(VAL,||);
        如果(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
        {
        LabelListField名单=(LabelListField)listField;
        g.setColor(0xC0C0C0);
        g.fillRect(0,Y + 0480,list.getRowHeight());
        如果(COL [指数] .equals(1))
        {         g.setColor(0x004D7B);
        }
        其他
        {             g.setColor(Color.GRAY);        }
        TableRowManager rowManager =(TableRowManager)list.mRows
                .elementAt(索引);        rowManager.drawRow(克,0,Y,宽度,list.getRowHeight());
        }
        其他
        {
            如果(COL [指数] .equals(1))
            {            g.setColor(0x004D7B);
            }
            其他
            {
            g.setColor(Color.GRAY);            }
            LabelListField名单=(LabelListField)listField;
            TableRowManager rowManager =(TableRowManager)list.mRows
                    .elementAt(索引);
            rowManager.drawRow(克,0,Y,宽度,list.getRowHeight());
        }    }    公共对象获取(ListField名单,INT指数){
        返回mValues​​.elementAt(索引);
    }    公众诠释indexOfList(ListField列表,字符串preFIX,INT启动){
        对于(INT X =启动; X< mValues​​.size(); ++ x){
            字符串值=(字符串)mValues​​.elementAt(X);
            如果(value.startsWith(preFIX)){
                返回X;
            }
        }
        返回-1;
    }    公众诠释的get preferredWidth(ListField名单){
        返回Display.getWidth();
    }    类ListLabel扩展{的LabelField
        INT MINDEX = -1;
        字符串文本;
        INT [] =色新INT [500];
        公共ListLabel(LabelListField名单,INT指数,字符串文本){
            超(文本);
            this.text =文本;
            MINDEX =指数;
            的System.out.println(定位是+位置);        }         公众诠释的get preferredWidth(){
             返回Display.getWidth() - 80;
         }         保护无效布局(INT了maxWidth,诠释了maxHeight){
             super.layout(GET preferredWidth()了maxHeight);
             setExtent(GET preferredWidth()的getHeight());
         }
    保护布尔navigationClick(INT的地位,诠释时间){
                 fieldChangeNotify(0);
                 返回true;
               }
               保护无效fieldChangeNotify(INT上下文){
                 如果(上下文== 0){
                   尝试{
                     。this.getChangeListener()fieldChanged(这一点,背景);
                   }赶上(例外五){}
                 }
               }
    }
    保护布尔trackwheelClick(INT的地位,诠释时间)
    {
        返回true;
    }
}


解决方案

scheduleInvalidate method.By去除无用的呼唤 scheduleInvalidate()正常工作

I am developing custom list field using Table Row Manager in blackberry.it displays list of items coming from web service.When i click on item it goes to other screen.But i am getting too many threads exception.after some time i am getting too many threads exception when i click on item.Then i check when the thread is creating by using debug.Then i found of displaying each item it creating separate thread.How can i solve this problem please help here is my custom list field 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;
    }
}

解决方案

useless Calling of scheduleInvalidate method.By removing scheduleInvalidate() it works fine

这篇关于而在黑莓使用自定义列表场线程太多例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆