2

以下代码用于将属性样式设置为 body 元素并向其注册剪切/粘贴代码,但失败。奇怪的是,在调试的时候,变量body不为空,并且属性“style”存在,但框架没有这个孩子。运行工作没有按预期工作,调用时 NPE 被抛出setBodyContent()

public class TextAreaExt extends RichTextArea
{
private BodyElement body;

@Override
protected void onLoad()
{
    super.onLoad();
    body = ((FrameElement) getElement().cast())
                .getContentDocument().getBody();
    body.setAttribute("style", "color:gray;");
    registerOnCut(body);
}

public void setBodyContent(String text)
{
    body.setInnerText(text);
}

private native void registerOnCut(BodyElement element) /*-{
    var that = this;
    console.log("registerOnCut");
    element.oncut = $entry(function(event) {
        //invoke method to adjust height based on content
        return false;
    });
    element.onpaste = $entry(function(event) {
        //invoke method to adjust height based on content
        return false;
    });
}-*/;
}
4

1 回答 1

0

不要调用onLoad方法来初始化主体,通过使用addInitializeHandler.

  this.addInitializeHandler(new InitializeHandler() {

        @Override
        public void onInitialize(InitializeEvent event) {
            body = ((FrameElement)getElement().cast()).getContentDocument().getBody();
            registerOnCut(body);
        }
    });
于 2014-03-02T18:40:44.947 回答