我正在 Flex 中制作 RTE,并尝试制作文本格式按钮。
<s:ToggleButton id="boldBtn" width="50" height="50" label="B" click="boldBtn_clickHandler(event)" color="#000000" fontWeight="bold"/>
和我的代码
protected function boldBtn_clickHandler(event:MouseEvent):void
{
var txtLayFmt:TextLayoutFormat = mainTextField.getFormatOfRange(null,
mainTextField.selectionAnchorPosition,
mainTextField.selectionActivePosition);
txtLayFmt.fontWeight = (txtLayFmt.fontWeight == FontWeight.BOLD) ? FontWeight.NORMAL : FontWeight.BOLD; **// Causing the NULL Pointer exception**
mainTextField.setFormatOfRange(txtLayFmt,
mainTextField.selectionAnchorPosition,
mainTextField.selectionActivePosition);
mainTextField.setFocus();
}
当我在 TextArea 中键入一些文本并选择它然后单击粗体时,我得到一个无法访问空对象引用的属性或方法。如果我注释掉 txtLayFmt.fontWeight = (txtLayFmt.fontWeight == FontWeight.BOLD) ?FontWeight.NORMAL : FontWeight.BOLD; 该程序不会崩溃,所以这似乎是违规行,但我不明白为什么。
编辑 我现在正在尝试这段代码。当我在桌面应用程序中使用它时它可以工作,但是当我将它放在移动项目中时它不起作用。有任何想法吗?
……
private function btnBold_click(evt:MouseEvent):void {
trace("Clicked"); // Traces to output ok
var styleObj:TextLayoutFormat = new TextLayoutFormat();
styleObj.fontWeight = FontWeight.BOLD;
mainTextField.setFormatOfRange(styleObj);
}
这段代码有什么问题?