Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Blackberry中控制manager类_Blackberry - Fatal编程技术网

如何在Blackberry中控制manager类

如何在Blackberry中控制manager类,blackberry,Blackberry,亲爱的各位,我在Blackberry上创建UI时遇到问题 首先,我尝试创建一个从Manager类扩展而来的ChatLayoutManager类。我的布局有三个组成部分:topfield、mainfield和bottom field public class ChatLayoutManager extends Manager { private Field bottomField; private Field mainField; private Field titl

亲爱的各位,我在Blackberry上创建UI时遇到问题

首先,我尝试创建一个从Manager类扩展而来的ChatLayoutManager类。我的布局有三个组成部分:topfield、mainfield和bottom field

public class ChatLayoutManager extends Manager {  
   private Field bottomField;  
   private Field mainField;  
   private Field titleField;  

   public ChatLayoutManager(long style) {  
       super(style);  
   }  

   protected void sublayout(int width, int height) {  
       setExtent(width, height);  

       int y = 0;  
       if (bottomField != null) {  
           layoutChild(bottomField, width, height);  
           // This goes at the bottom of the screen  
           setPositionChild(bottomField, 0, height-bottomField.getHeight());  
           height -= bottomField.getHeight();  
       }  

       if (titleField != null) {  
           layoutChild(titleField, width, height);  
           // This goes at the top of the screen  
           setPositionChild(titleField, 0, 0);  
           height -= titleField.getHeight();  
           y += titleField.getHeight();  
       }  

       if (mainField != null) {  

           layoutChild(mainField, width, height);  
           // This goes just below the title field (if any)  
           setPositionChild(mainField, 0, y);  
       }  

   }  

   public void setMainField(Field f) {  
       mainField = f;  
       add(f);  
   }  

   public void setBottomField(Field f) {  
       bottomField = f;  
       add(f);  
   }  

   public void setTitleField(Field f) {  
       titleField = f;  
       add(f);  
   }  
然后,我创建了另一个字段(ChatField),该字段从manager扩展到我上面创建的ChatLayoutManager类中的mainfield

public class ChatField extends Manager{

private Field _contentField[];

protected ChatField(){
    super(Manager.HORIZONTAL_SCROLL | Manager.VERTICAL_SCROLL);
}
// TODO Auto-generated constructor stub}

protected synchronized void sublayout(int width, int height) {
    // TODO Auto-generated method stub
    setExtent(width, height);
    int x = 0;
    int y = 0;

    if(_contentField.length > 0){
        for(int i = 0 ;i<_contentField.length; i++){
            //if(getManager() == this){
                this.layoutChild(_contentField[i],
                            _contentField[i].getWidth(), 
                            _contentField[i].getHeight());
                this.setPositionChild(_contentField[i], x, y);
                if(_contentField[i++]!= null){
                    if ((_contentField[i].getWidth() + _contentField[i].getWidth())
                            >= width){
                        x = 0;
                        y += _contentField[i].getHeight();
                    }
                    else{
                        x += _contentField[i].getWidth();

                    }
                }
            //}
        }

    }

}

public void setContentField(Field field[]){
    _contentField = field;
}
公共类ChatField扩展管理器{
私有字段_contentField[];
受保护的聊天字段(){
超级(Manager.HORIZONTAL|u SCROLL | Manager.VERTICAL_SCROLL);
}
//TODO自动生成的构造函数存根}
受保护的同步空位子布局(整数宽度、整数高度){
//TODO自动生成的方法存根
设置范围(宽度、高度);
int x=0;
int y=0;
如果(_contentField.length>0){
对于(int i=0;i=width){
x=0;
y+=\u contentField[i].getHeight();
}
否则{
x+=\u contentField[i].getWidth();
}
}
//}
}
}
}
public void setContentField(字段[]){
_contentField=field;
}
}

现在,当我创建一些添加到ChatField的字段(如TextField、BitmapField…)时,程序出现了一个错误“Field不是这个管理器的子项”。原因是当框架调用ChatField类的sublayout函数时,当sublayout开始调用layoutChild函数时,字段的管理器不是ChatField,而是ChatlayoutManager

我经历了解决这个问题的艰难时期,但我仍然没有解决办法。谁能给我一些建议?非常感谢。

当您调用Manager.add()(传入字段)时,该字段将成为该经理的子字段。一个字段只能是一个管理器的子字段-它不能属于多个管理器。如果出现该错误,则可能是意外地将其添加到多个管理器中。

您的setContentField(Field[])函数获取对提供的字段数组的引用,但它从不将数组的内容添加到管理器(ChatField)。这就是为什么会出现这样的错误:该字段不是此管理器的子项

其次,关于ChatField子布局,您应该执行以下操作之一:

1) 使用Manager提供的功能来引用其子级,而不是直接引用_contentField数组:

int numFields = getFieldCount(); // getFieldCount() is a member of Manager
int marginHorizontal = 0;
for(int i = 0; i < numFields; i++) {
    field = getField(i); // getField() is a member of Manager
    // Whatever you need to do to it.
}
int numFields=getFieldCount();//getFieldCount()是Manager的成员
int marginHorizontal=0;
对于(int i=0;i
2) 如果您仍然希望直接引用_contentField,或者如果您需要控制某个特定字段,那么请确保该字段的管理者就是正在布置的管理者:

for(int i = 0 ;i<_contentField.length; i++){
    if(_contentField[i] != null && _contentField[i].getManager() == this){
        // Whatever you need to do to it.
    }
}

对于(inti=0;iagree,'if(_contentFields[i++]!=null){'代码中也有一个bug,最好使用'if(i-1<_contentFields.lenght){'