1

mxml程序出现如下错误如何解决?

错误是

       Loading configuration file /opt/flex/frameworks/flex-config.xml
       /home/tom-j/programs/flex/html/aa.mxml(17):  Error: Access of undefined property addBody.

       ExternalInterface.addCallback("addBody", addBody);

Program is

     <?xml version="1.0" encoding="utf-8"?>
     <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

     <mx:Script>
     <![CDATA[

     import mx.controls.Button;
     import mx.controls.Alert;
     import flash.display.InteractiveObject;
     import flash.display.Sprite;
     import flash.media.*;
     import flash.net.*;
     import flash.external.*;
     import flash.external.ExternalInterface;

     //                "javascript function", flash function
     ExternalInterface.addCallback("addBody", addBody);

     public function addBody():void
     {
       Alert.show("Got input from JS");
     }
         ]]>                
     </mx:Script>
     </mx:Application>
4

1 回答 1

2

您应该尝试将此调用放入 onCreationComplete 事件处理程序中:

protected function onCreationComplete(event:FlexEvent):void
{
    //just in case to prevent security exceptions:
    Security.allowDomain("*.yourdomain.com");
    Security.allowDomain("localhost");
    ExternalInterface.addCallback("addBody", addBody);
}

引发的错误可能是因为在处理该addCallback行时,该方法不存在addBody

更新addBody从 javascript调用方法

在您的 html 中,您必须嵌入 swf 〜像这样:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="your_flash_app" width="1000" height="520"
    codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
    <param name="movie" value="YourFlashApp.swf" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#bee3f6" />
    <param name="allowScriptAccess" value="always" />
    <embed src="YourFlashApp.swf" quality="high" bgcolor="#bee3f6" width="1000" height="520" name="your_flash_app" align="middle" play="true" loop="false"
        quality="high" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer">
    </embed>
</object>

要从 javascript 调用该addBody方法,首先您必须在那里找到 flash 应用程序:

//this only works for sure in IE browsers, but there are workarounds 
//to deal with the others.
var flashApp = document.getElementById("your_flash_app");
flashApp.addBody();
于 2011-04-15T10:56:02.587 回答