我正在尝试从其他类向主屏幕添加文本,我试图从扩展MainScreen的类传递屏幕对象但是当我尝试向其添加内容时,它会给我Permission denied异常。添加例如从不同类别向主屏幕提交的文本的正确方法是什么?
编辑:
public TheMainClass扩展了MainScreen {
public TheMainClass() { LabelField labelField = new LabelField("Hello"); add(labelField); } }
public OtherClass {
public OtherClass(){ // i want to add new LabelField here to say for example "World!" to the // TheMainClass screen } }
答案 0 :(得分:2)
有很多方法可以做到这一点,一个是:
public TheMainClass extends MainScreen {
public TheMainClass() {
LabelField labelField = new LabelField("Hello");
add(labelField);
}
}
public OtherClass {
public addLabelTo(Screen aScreen) {
aScreen.add(new LabelField("World!"));
}
}
TheMainClass theMainClass = new TheMainClass();
OtherClass otherClass = new OtherClass;
otherClass.addLabelTo(theMainClass);
当然,您必须确保在事件线程上执行对addLabelTo的调用。