我在网上搜索过,但找不到问题的答案。 我正在构建一个读取txt文件并在空中应用程序中显示它们的空中应用程序,但由于一个奇怪的原因,我在同一行上出现了2个错误。
错误:
-1067:将void类型的值隐式强制转换为不相关的类型函数。
-1067:将类型Class的值隐式强制转换为不相关的类型flash.events:Event。
代码:
public function init():void {
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
public function txtReeks1_creationCompleteHandler():void {
var url:String = "Files/Reeks1.txt";
var loadit:URLLoader = new URLLoader();
loadit.addEventListener(Event.COMPLETE, completeHandler(Event));
loadit.load(new URLRequest(url));
}
public function completeHandler(event:Event):void {
txtReeks1.text = event.target.data;
stripLinesFromTextArea(txtReeks1);
}
public function stripLinesFromTextArea(textArea:TextArea):void {
var txt:String = textArea.text;
var re:RegExp = /\n+/g;
txt = txt.replace(re,"");
textArea.text = txt;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:TabNavigator x="0" y="0" width="100%" height="100%">
<mx:Canvas id="Reeks1" width="100%" height="100%" label="Reeks1">
<mx:TextArea id="txtReeks1" x="0" y="0" width="100%" height="100%"
creationComplete="txtReeks1_creationCompleteHandler()"
fontFamily="Lucida Console" fontSize="20"/>
</mx:Canvas>
<mx:Canvas id="Reeks2" width="100%" height="100%" label="Reeks2">
</mx:Canvas>
<mx:Canvas id="Reeks3" width="100%" height="100%" label="Reeks3">
</mx:Canvas>
</mx:TabNavigator>
</mx:WindowedApplication>
我希望有人可以帮助我,因为我在最近2天寻找答案
答案 0 :(得分:3)
loadit.addEventListener(Event.COMPLETE, completeHandler(Event));
这就是问题所在。您正在调用处理程序并将Event
类传递给它。添加监听器时,您只需要引用该函数,而不是调用它:
loadit.addEventListener(Event.COMPLETE, completeHandler);
答案 1 :(得分:3)
问题出在您的txtReeks1_creationCompleteHandler()
方法中,如下所示:
loadit.addEventListener(Event.COMPLETE, completeHandler(Event));
第二个参数应该是对函数的引用,而不是函数本身的执行 - 它返回void。所以它应该如下所示:
loadit.addEventListener(Event.COMPLETE, completeHandler);
答案 2 :(得分:0)
您的错误就行了:
loadit.addEventListener(Event.COMPLETE, completeHandler(Event));
您不应该将函数的结果传递给事件处理程序,而是函数本身:
loadit.addEventListener(Event.COMPLETE, completeHandlerEvent);