Actionscript 3 在标签中,未显示模块中使用的值

Actionscript 3 在标签中,未显示模块中使用的值,actionscript-3,apache-flex,flex3,Actionscript 3,Apache Flex,Flex3,我试图调用模块的一个函数(有两个参数)并为其赋值。 我可以在跟踪中看到分配的值,但在分配给标签时,相同的值不会显示 如果我直接将值指定给标签。它说: 错误:1009:空对象引用 如果对该标签使用[Bindable]元标记,则不会显示任何运行时错误,但也不会显示该值。我在互联网上搜索发现,[Bindable]元标记可以作为try-and-catch-blog使用 如何将值显示指定给标签 Main.mxml <?xml version="1.0" encoding="utf-8"?>

我试图调用模块的一个函数(有两个参数)并为其赋值。 我可以在跟踪中看到分配的值,但在分配给标签时,相同的值不会显示

  • 如果我直接将值指定给标签。它说:

    错误:1009:空对象引用

  • 如果对该标签使用
    [Bindable]
    元标记,则不会显示任何运行时错误,但也不会显示该值。我在互联网上搜索发现,
    [Bindable]
    元标记可以作为try-and-catch-blog使用

  • 如何将值显示指定给标签

    Main.mxml
    
     <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"   xmlns:custom="com.custom.*" xmlns:local="*" creationComplete="onload()">
      <mx:Script>
    <![CDATA[
        import modules.AModule; 
        import mx.events.ModuleEvent;
        import mx.containers.VBox;
        import mx.containers.TitleWindow;
        import mx.controls.TextInput;
        import mx.controls.Button;
        import mx.containers.HBox;
        import mx.controls.Label;
        import mx.events.CloseEvent;
        import mx.controls.Text;
        import com.custom.CustomAlert;
        import mx.controls.Alert;
        import mx.containers.Canvas;
        import mx.events.ItemClickEvent;
    
        import mx.core.UIComponent;
    
        import mx.modules.ModuleManager;
        import mx.modules.IModuleInfo;
        import mx.containers.Box;
        import mx.controls.ProgressBar;
    
        //private var can:Canvas;
    
        public var titlewin:TitleWindow = new TitleWindow();
            public var usern:TextInput = new TextInput();
            public var passw:TextInput = new TextInput();
            public var vb:VBox = new VBox();
            public var hb1:HBox = new HBox();
            public var hb2:HBox = new HBox();
            public var hb3:HBox = new HBox();
            public var nc:NetConnection;
    
            public var bt1:Button = new Button();
    
            private var pb:ProgressBar;
        private var progressContainer:Box;
        private var info:IModuleInfo;
        private var module:UIComponent = null;
        public var can:Canvas= new Canvas();
        public function onload():void
        {
                var label1:Label= new Label();
            var label2:Label= new Label();
            label1.text="Username";
            label2.text="Password ";
            hb1.addChild(label1);
            hb1.addChild(usern);
            hb2.addChild(label2);
            hb2.addChild(passw);
            vb.addChild(hb1);
            vb.addChild(hb2);           
            bt1.label="Sign In";
            hb3.addChild(vb);
            hb3.addChild(bt1);
            titlewin.addChild(hb3);
    
            //Title Window creation starts here
            titlewin.title="Login";
    
            titlewin.setStyle("paddingLeft",20);
            titlewin.setStyle("paddingRight",20);
            titlewin.setStyle("paddingTop",20);
            titlewin.setStyle("paddingBottom",20);
            titlewin.setStyle("borderThicknessBottom",0);
            titlewin.setStyle("borderThicknessLeft",0);
            titlewin.setStyle("borderThicknessRight",0);
            titlewin.setStyle("borderThicknessTop",0);
            titlewin.setStyle("cornerRadius",0);
            titlewin.setStyle("borderColor","#D20101");
            this.addChild(titlewin);
            viewStack.enabled=false;
            ctbb.enabled=false;
            bt1.addEventListener(MouseEvent.CLICK, netconn);
    
        }
    
        private function onclick():void
        {
            if(usern.text!="")
            {
        this.removeChild(titlewin);
        viewStack.enabled=true; 
        ctbb.enabled=true;
        username.text=usern.text;
            }
            else
            {
                Alert.show("Please enter Username");
            }
        }
    
    
        private function netconn(event:MouseEvent):void
        {
            nc = new NetConnection();
            nc.connect("rtmp://localhost/loginhere",usern.text);
            nc.addEventListener(NetStatusEvent.NET_STATUS, onstatushandler);
    
        }
    
        private function onstatushandler(event:NetStatusEvent):void
        {
            if(event.info.code=="NetConnection.Connect.Success")
            {
                status.text="Connected";
            onclick();  
                }
    
    
            if(event.info.code=="NetConnection.Connect.Rejected")
            {
                status.text="Rejected";
            }
            if(event.info.code=="NetConnection.Connect.Failed")
            {
                    status.text="Failed";
            }
        }
    
    
    
        private function JoinClick(event:MouseEvent):void{
    
            if(viewStack.numChildren==3)
            {
                Alert.show("You Cant Add More Button","Alert");
    
            }
    
            if(viewStack.numChildren<3)
            {
    
            if(viewStack.numChildren<2)
            {
            can.label="Button2";
            var button1:CustomButton= new CustomButton();
            button1.label="Leave Table";
            button1.x=300;
            button1.y=200;
            var txt:Text = new Text();
            txt.text=can.label;
            txt.x=50;
            txt.y=50;
            button1.addEventListener(MouseEvent.CLICK,remove2);
            can.addChild(button1);
            can.addChild(txt);
            loadmodule();
            }
            else
            {
    
            can.label="Button3";
            var button2:CustomButton= new CustomButton();
            button2.label="Leave Table";
            button2.x=300;
            button2.y=200;
            button2.addEventListener(MouseEvent.CLICK,remove3);
            can.addChild(button2);
            var txt1:Text = new Text();
            txt1.text=can.label;
            txt1.x=50;
            txt1.y=50;
            can.addChild(txt1);
            }
            can.setStyle("backgroundColor","0x5EA5BA");
            viewStack.addChild(can);
            if(viewStack.numChildren>2)
            {
                viewStack.selectedIndex=2;
            }
            else
            {
            viewStack.selectedIndex=1;
            }
            }
    
            }
    
    
            private function remove2(event:MouseEvent):void{
            Alert.show("Do you want to Leave    Table?","Confirmation",Alert.YES|Alert.NO,this,closeAlert2);
            }
            private function closeAlert2(event:CloseEvent):void{
            if(event.detail==Alert.YES)
            {
            viewStack.removeChildAt(1);
            }
    
            }
            private function remove3(event:MouseEvent):void{
            if(viewStack.numChildren>2)
            {
                Alert.show("Do You want to leave Table?","Confirmation",Alert.YES|Alert.NO,this,closeAlert3);
    
            }
            else
            {
                Alert.show("Do you want to Leave Table?","Confirmation",Alert.YES|Alert.NO,this,closeAlert1);
    
            }
            }
    
            private function closeAlert1(event:CloseEvent):void{
                if(event.detail==Alert.YES)
                {
                    viewStack.removeChildAt(1);
    
                }
            }
            private function closeAlert3(event:CloseEvent):void{
                if(event.detail==Alert.YES)
                {
                        viewStack.removeChildAt(2);
                }
            }
    
    
                private function loadmodule():void
        {
                pb = new ProgressBar();
                pb.labelPlacement = "center";
                pb.label = "Loading %3 %";
                progressContainer = new Box();
                progressContainer.percentWidth = 100;
                progressContainer.percentHeight = 100;
                progressContainer.setStyle("horizontalAlign", "center");
                progressContainer.setStyle("verticalAlign", "middle");
                progressContainer.addChild(pb);  
                can.addChild(progressContainer);
    
    
            var moduleUrl:String="../bin-debug/modules/useModule.swf";
            info=ModuleManager.getModule(moduleUrl);
            if(info!=null)
            {
                info.addEventListener(ModuleEvent.READY, modEventHandler);
                info.addEventListener(ModuleEvent.ERROR,modErrorHandler);
                info.load();
            }
        }
    
    
    
        private function modEventHandler(e:ModuleEvent):void
        {
            info.removeEventListener(ModuleEvent.READY, modEventHandler);
            info.removeEventListener(ModuleEvent.ERROR, modErrorHandler);
            can.removeAllChildren();
            if(module==null)
            {
    
                module = info.factory.create() as UIComponent;
                if(module!=null)
                { 
                    module.x=0;
                    module.y=0;
                    module.percentWidth=100;
                    module.percentHeight=100;
                    can.addChild(module);
                     if(module is AModule)
                    {
    
                    } 
                }
            }
            }
    
                private function modErrorHandler(event:ModuleEvent):void {
    
    
                //cleanup and display an error alert
                info.removeEventListener(ModuleEvent.READY, modEventHandler);
                info.removeEventListener(ModuleEvent.ERROR, modErrorHandler);
                info = null;
                Alert.show(event.toString(), "Error Loading Module");
    
              unloadModule();
            }   
                    private function unloadModule():void 
                    {
               can.removeAllChildren();
                if (module != null && info != null) {
                    info.addEventListener(ModuleEvent.UNLOAD,    unloadEventHandler);
                    info.unload();
                }
            }
    
                    private function unloadEventHandler(e:ModuleEvent):void 
                    {
                info.removeEventListener(ModuleEvent.UNLOAD, unloadEventHandler);
                module = null;
                info = null;
                     }
    
    
    ]]>
      </mx:Script>
    
     <mx:Canvas width="700" height="400" x="166" y="32">
     <mx:ViewStack id="viewStack"  width="100%" height="100%">
        <mx:Canvas label="Button1" backgroundColor="#5EA5BA" visible="true" id="can1" >
        <local:CustomButton label="Join" click="JoinClick(event)"  x="536" y="222"/>
        <mx:Label x="304" y="120" text="Label" id="username"/>
        <mx:Label x="304" y="55" text="Label" id="status"/>
        </mx:Canvas> 
    </mx:ViewStack>
    
     </mx:Canvas>
      <custom:CustomControlBar x="166" y="423" id="ctbb">
      <custom:CustomToggleButtonBar  dataProvider="viewStack"/>
      </custom:CustomControlBar>
    
      </mx:Application>
    

    我还没有看完整个代码,但我注意到一件事,您应该将值作为参数传递给子组件,并使用数据绑定动态更新参数

    因此,在AModule.as中:

    <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="508"  height="218" implements="modules.AModule" backgroundColor="#F00F0F"  creationComplete="addlabel();">
    <mx:Script>
        <![CDATA[
            import mx.core.Application;
    
            public var logsin:loginhere = new loginhere();
    
            [Bindable]
            public var username:String = "";
        ]]>
    </mx:Script>
    
        <mx:TextInput id="txt3" text="{username}" x="30.5" y="10" width="79"/>
        <mx:Label id="lb1" text="{username}" x="31.5" y="40" width="78" text="Will it Change?"/>
    </mx:Module> 
    

    我遗漏的一点是,我无法在main.mxml中为接口创建一个实例 应在
    modEventHandler
    函数中创建var。在该函数中,应添加以下行

    ichild=module as AModule;
    
    ichild.addlabel(tt.text);
    
    因此,将调用该模块,并在标签字段中显示该值


    请让我知道,如果这是错误的工作方式。谢谢

    请向我们展示您使用的代码-这将使我们更容易提供帮助。感谢您的回复。它太复杂了,无法显示为,因为代码从服务器获取数据并执行其余操作。你能帮我举个例子吗,因为我需要从主文件向模块中的函数(有两个参数)传递值并加载模块。Thankstory@JcFx,我已经更新了我的问题,如果你不理解我的问题,请告诉我。谢谢你+1这是一个好问题。我无法立即看到是什么导致了您的错误,但希望其他人能够提供帮助。Module.mxml中是否存在问题?我看到您在那里创建了一个新的Canvas1元素,而没有传递一个新字符串来设置它的标签。我遗漏的一点是,我无法创建一个变量,变量应该在
    modEventHandler
    函数中创建
    在该函数中,下面的一行应该作为AModule添加
    ichild=module;ichild.addlabel(tt.text)
    因此调用模块,并在标签字段中显示值。请让我知道,如果这是错误的工作方式。
    <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="508"  height="218" implements="modules.AModule" backgroundColor="#F00F0F"  creationComplete="addlabel();">
    <mx:Script>
        <![CDATA[
            import mx.core.Application;
    
            public var logsin:loginhere = new loginhere();
    
            [Bindable]
            public var username:String = "";
        ]]>
    </mx:Script>
    
        <mx:TextInput id="txt3" text="{username}" x="30.5" y="10" width="79"/>
        <mx:Label id="lb1" text="{username}" x="31.5" y="40" width="78" text="Will it Change?"/>
    </mx:Module> 
    
    module.username = //var that holds username;
    
    ichild=module as AModule;
    
    ichild.addlabel(tt.text);