Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
UberschallSamsara
Posts: 1,007
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: Add EventListener

I just meant the parameter type of the event listener function, i.e.

 

public function myEventHandler(evt:<parameter type>):void {

   // event handling code

}

 

where <parameter type> is the data type of the evt parameter of the myEventHandler function.

Event handler function examples I've seen to date expect this data type to be Event in the case of a Deactivate event, MouseEvent for mouse clicks, etc.

 

Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Add EventListener

You can always use the Event class for the response parameter type.  Unless you need to access an attribute of the event class, using the base class Event will always work.

Please use plain text.
Developer
UberschallSamsara
Posts: 1,007
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: Add EventListener

Yep I was just wondering about the OP's example code

 

http://supportforums.blackberry.com/t5/Tablet-OS-SDK-for-Adobe-AIR/Add-EventListener/m-p/791649#M960...

 

since it looked like the handler function param type wasn't an actual event class.

 

By the way, Original Poster: are you getting your error message about 'access of undefined property onDeactivate" at compile time or at run time?  (I should be able to guess this but I've been spending too much time reading and not enough time coding).

Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Add EventListener

hey sky,

 

here is a working flex example of how to use the activate / deactivate events. use it as a guide and translate it to how your app works:

 

FlexTestHome.mxml:

 

 

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
		xmlns:s="library://ns.adobe.com/flex/spark" title="Home" creationComplete="init()">
	<fx:Style>
		@namespace s "library://ns.adobe.com/flex/spark";
		
		s|Label#myLabel {
			font-size: 72;
			font-weight: bold;
			font-family: "BBAlpha Sans";
		}
	</fx:Style>
	<fx:Script>
		<![CDATA[
			
			private function init():void
			{
				/** Just setting up objects to show the deactivation / activation */
				
				coverOverlay.graphics.clear();
				coverOverlay.graphics.beginFill(0xFF0000);
				coverOverlay.graphics.drawRect(0,0,coverOverlay.width,coverOverlay.height);
				coverOverlay.graphics.endFill();
				

				myLabel.text = "Application Deactivated!";
				myLabel.x = (coverOverlay.width / 2) - (myLabel.measureText(myLabel.text).width / 2);
				myLabel.y = (coverOverlay.height / 2) - (myLabel.measureText(myLabel.text).height / 2);
				
				/** 
				 * add the listener to your main application to listen for the deactivation
				 * when your app gets minimized
				 */
				
				this.addEventListener(Event.DEACTIVATE, onDeactivate);
			}
			
			
			private function onDeactivate(e:Event):void
			{
				/** show the new screen when the screen deactivates */
				
				coverOverlay.visible = true;				
				myLabel.visible = true;

				
				/** remove the listener, good practice when not in use */
				this.removeEventListener(Event.DEACTIVATE, onDeactivate);
				
				/** 
				 * add the listener for the activation event when the application
				 * gets maximized
				 */
				this.addEventListener(Event.ACTIVATE, onActivate);
			}
			
			private function onActivate(e:Event):void
			{
				/**
				 * when your application gets maximized, remove the
				 * red screen from view
				 */
				
				if (coverOverlay.visible)
				{
					coverOverlay.visible = false;	
					myLabel.visible = false;
				}
				
				/** remove the listener for the activate, no longer needed */
				
				this.removeEventListener(Event.ACTIVATE, onActivate);
				
				/** re-add the listener for when the app gets minimized */
				
				this.addEventListener(Event.DEACTIVATE, onDeactivate);
			}
			
		]]>
	</fx:Script>

	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<s:SpriteVisualElement id="coverOverlay" visible="false" width="1024" height="600" />
	<s:Label id="myLabel" visible="false" color="0x000000" />
</s:View>

 

hope that clears a few things up. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.
Developer
UberschallSamsara
Posts: 1,007
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: Add EventListener

By the way, sky's code example imports mx.events.FlexEvent but in the ASDocs I don't see ACTIVATE/DEACTIVATE constants under that class - it has FLEX_WINDOW_ACTIVATE/FLEX_WINDOW_DEACTIVATE constants though.

 

sky are you not importing flash.events.Event class? 

Please use plain text.
Contributor
sky858
Posts: 36
Registered: ‎02-12-2011
My Device: Not Specified

Re: Add EventListener

Thank you for the replies everyone.  The event listener is working now (I had the addEventListener part in the wrong place).  The problem I am having now is I am getting error 1009 cannot access a property or method of a null object reference.  The problem seems to be with the sharedobject that the eventListener saves.  It is a very odd error though because it only occurs sometimes and it will work perfectly other times.  I think what is causing it is I am using a button bar with a viewstack and the textAreas that are saved by sharedObject are within the seperate sections of the viewstack.  Is there a simple way to avoid this error?

Please use plain text.
Developer
UberschallSamsara
Posts: 1,007
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: Add EventListener

Try setting a breakpoint on any line of code that assigns a value to your shared object variable, to get a better idea of where it is going null.

Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Add EventListener

Make certain that the item you are trying to access was allocated at some point.  You can also check if it is null or not.  If you are getting that error, there is a slip in the logic somewhere.

Please use plain text.
Developer
UberschallSamsara
Posts: 1,007
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C

Re: Add EventListener

Alternatively or additionally you can set a watchpoint on whichever variable you suspect is getting a null reference, instead of setting breakpoints on all lines of code that assign to it:

 

Using Watchpoints

Please use plain text.
Contributor
sky858
Posts: 36
Registered: ‎02-12-2011
My Device: Not Specified

Re: Add EventListener

Ok, I am very confused on why it isn't working now.  Here is more of my code.  The debug points to so.data.g1 = g1.text when the error happens.  The odd thing is it doesnt throw the error all the time.  I didn't show all of the navigatorContent and every textArea below, but they are the same as the first two that are shown.  Sorry for all the questions, but I really do appreciate your help

 

 

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                       xmlns:s="library://ns.adobe.com/flex/spark"
                       xmlns:mx="library://ns.adobe.com/flex/mx" showStatusBar="false" backgroundColor="#000000" xmlns:ns1="*" xmlns:events="qnx.events.*">
    <fx:smileyfrustrated:tyle source="MasterPlanAlt.css"/>
    <fx:smileyfrustrated:cript>
        <![CDATA[
            import flash.events.Event;
            
            import mx.events.FlexEvent;
            
            [Bindable]
            private var so:smileyfrustrated:haredObject;
            [Bindable]
            private var text_g1:smileyfrustrated:tring;
            [Bindable]
            private var text_g2:smileyfrustrated:tring;
            [Bindable]
            private var text_g3:smileyfrustrated:tring;
            [Bindable]
            private var text_g4:smileyfrustrated:tring;
            [Bindable]
            private var text_g5:smileyfrustrated:tring;
            [Bindable]
            private var text_g6:smileyfrustrated:tring;
            [Bindable]
            private var text_g7:smileyfrustrated:tring;
            [Bindable]
            private var text_g8:smileyfrustrated:tring;
            [Bindable]
            private var text_g9:smileyfrustrated:tring;
            [Bindable]
            private var text_g10:smileyfrustrated:tring;
            [Bindable]
            private function onDeactivate(e:Event):void
            {
                so.data.g1 = g1.text;
                so.data.g2 = g2.text;
                so.data.g3 = g3.text;
                so.data.g4 = g4.text;
                so.data.g5 = g5.text;
                so.data.g6 = g6.text;
                so.data.g7 = g7.text;
                so.data.g8 = g8.text;
                so.data.g9 = g9.text;
                so.data.g10 = g10.text;
                so.flush();
            }
        ]]>
    </fx:smileyfrustrated:cript>
    <s:creationComplete>
        <![CDATA[
        so = SharedObject.getLocal("userData");
        {
            text_g1 = so.data.g1;
            text_g2 = so.data.g2;
            text_g3 = so.data.g3;
            text_g4 = so.data.g4;
            text_g5 = so.data.g5;
            text_g6 = so.data.g6;
            text_g7 = so.data.g7;
            text_g8 = so.data.g8;
            text_g9 = so.data.g9;
            text_g10 = so.data.g10;
            this.addEventListener(Event.DEACTIVATE, onDeactivate);

        }
    ]]>
    </s:creationComplete>

<mx:ViewStack x="10" y="140" id="viewstack1" width="1001" height="458">
        <s:NavigatorContent label="1" width="100%" height="100%" id="nc1">

                 <s:BorderContainer x="10" y="28" width="981" height="105" backgroundColor="#000000">

                 <s:TextArea x="10" y="10" height="83" width="922" contentBackgroundColor="#CCCCCC"   fontSize="14" id="g1" text="{text_g1}"/>

        <s:NavigatorContent label="2" width="100%" height="100%" id="nc2">

                 <s:BorderContainer x="10" y="28" width="981" height="105" backgroundColor="#000000">

                 <s:TextArea x="10" y="10" height="83" width="922" contentBackgroundColor="#CCCCCC"   fontSize="14" id="g2" text="{text_g2}"/>

</mx:ViewStack>
    <mx:ToggleButtonBar x="10" y="91" dataProvider="{viewstack1}" width="1001" height="41" color="#000000" fontSize="14">
    </mx:ToggleButtonBar>

</s:WindowedApplication>

Please use plain text.