Return value instead of null - apache-flex

I'm trying to call a function and pass a couple of properties to it however it's complain the object i'm attempting to target is null. Can anyone see where I am going wrong?
<mx:ViewStack id="vs" width="100%" height="100%" y="53">
<mx:Canvas id="view1" label="Community" width="100%" height="100%" backgroundColor="#ff9900" showEffect="WipeDown" hideEffect="WipeUp">
<mx:Label text="Community"/>
</mx:Canvas>
<mx:Canvas id="view2" label="Apps" width="100%" height="100%" backgroundColor="green">
<mx:HTML id="myHTML" width="100%" height="100%"
visible="true"
paintsDefaultBackground="true"
htmlRender="browser_completeHandler(event)"
locationChange="browser_locationChangeHandler(event)"
complete="browser_completeHandler(event)" />
</mx:Canvas>
</mx:ViewStack>
<local:DockBar id="dockbar" horizontalCenter="0" bottom="0" width="100%" height="100" minSize="32" maxSize="80">
<mx:Label visible="false" id="menuLabel" text="Menu" bottom="0" horizontalCenter="0" fontSize="24" color="#ffffff" alpha=".75" />
<mx:Image click="gotoApp('Google','http://www.google.com/')" source="{icon1}" buttonMode="true" useHandCursor="true" toolTip="Nice red" rollOver="turnOn(event)" rollOut="turnOff(event)" />
<mx:Image click="gotoApp('Yahoo','http://www.yahoo.com/')" source="{icon2}" buttonMode="true" useHandCursor="true" toolTip="Cool orange" rollOver="turnOn(event)" rollOut="turnOff(event)" />
</mx:HBox>
</local:DockBar>
and the function looks like this:
private function gotoApp(id:String,url:String):void {
vs.selectedChild=view4;
trace(myHTML);
}
It's returning null the first time I click an image however subsequent attempts traces a value (I assume because it is set then, just not when the app loads). Any ideas how to recognize it when the app loads?
Cheers

Well "view4" isn't a child of your viewstack, so its going to throw a null pointer when it tries to shift its children.
The reason it doesn't throw it the second time is probably due to something like
public function set selectedChild( child : Object ) : void {
if ( child == _selectedChild ) return;
...
}
That's a pretty common pattern in setters.

Assuming "view4" is a typo and you meant "view2", you can set the creationPolicy on your ViewStack to "all". By default, ViewStacks only instantiate the first view when created, setting the creationPolicy to "all" will force all of the views in the stack to get instantiated.

The ViewStack will, by default, create its children lazily in document-order, so only the first child which will be visible on load (i.e view1) will not have a null id in your function. Subsequent clicks on the ViewStack will create the other views (view2, view3 and view4) which is why the error will no longer occur.
So you need to modify your code to include a creationPolicy="all" to fix this:
<mx:ViewStack id="vs" creationPolicy="all" width="100%" height="100%" y="53">
<mx:Canvas id="view1" label="Community" width="100%" height="100%">
...
</mx:Canvas>
...
<mx:Canvas id="view4" label="Apps" width="100%" height="100%">
...
</mx:Canvas>
</mx:ViewStack>

Try setting by using the selectedIndex property instead:
vs.selectedIndex = 1;
This way you're not losing out on the benefits of the deferred instantiation.

Related

Flex: Getting rid of "Warning: Filter will not render. The DisplayObject's filtered dimensions ..." error

When printing (I think at the point I call FlexPrintJob.addObject()) I get the following:
Warning: Filter will not render. The DisplayObject's filtered dimensions (3324, 1740) are too large to be drawn.
I don't know what it thinks is 3324, 1740. The mx:Box object I pass to it is about 600x100.
I'm wondering if this problem is related to another problem I'm having. What gets printed has an extra border on the bottom and the right of what I want printed. I'm hoping that understanding this message will correct my problem.
I'm using the Flex 3.5 SDK
Here's a stripped down version of the code that still allows for this problem:
<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml"
title="FPL Flight Strip"
showCloseButton="true"
borderAlpha="1"
borderColor="{BrandGlobals.COLOUR_DARK}"
borderThicknessBottom="1"
borderThicknessLeft="1"
borderThicknessRight="1"
borderThicknessTop="0"
dropShadowEnabled="true"
fontSize="{AppGlobals.fntSize}"
backgroundAlpha="1.0"
alpha="1.0"
creationComplete="init()"
close="PopUpManager.removePopUp(this);">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
import mx.printing.FlexPrintJobScaleType;
import mx.printing.FlexPrintJob;
private function init():void
{
this.setStyle('fontFamily', 'Arial');
}
private function printData():void
{
var dbPrintJob: FlexPrintJob = new FlexPrintJob();
if (dbPrintJob.start())
{
try
{
dbPrintJob.addObject(boxPrint, FlexPrintJobScaleType.NONE);
}
catch (e:Error)
{
//trace(e);
}
dbPrintJob.send();
}
}
]]></mx:Script>
<mx:VBox width="600" height="100" horizontalAlign="center" verticalAlign="middle" backgroundColor="#FFFFFF" verticalGap="1" paddingLeft="2" paddingRight="2" paddingTop="2" paddingBottom="2">
<mx:Box id="boxPrint" width="100%" height="100%" backgroundColor="#FFFFFF">
<mx:Box id="box" width="100%" height="100%"/>
</mx:Box>
</mx:VBox>
<mx:ControlBar width="100%" barColor="#E8E8E8" paddingBottom="0" paddingLeft="0" paddingRight="0" paddingTop="0">
<mx:VBox width="100%" height="100%" horizontalAlign="left" backgroundColor="#E8E8E8" paddingTop="5" paddingBottom="5" paddingRight="20" paddingLeft="20">
<mx:HBox width="100%" height="100%">
<mx:Button label="Print Strip" fontFamily="Arial" click="printData()" width="100" height="25" />
</mx:HBox>
</mx:VBox>
</mx:ControlBar>
</mx:TitleWindow>
I was not able to reproduce the filter error in Flex 3.6. Are you not able to upgrade? To my knowledge, there shouldn be very few issues with backwards compatibility, unless you are heavily overriding mx_internal.
I did however get the problem with the borders. This has to do with the way you are adding your objects. It's best to wrap the thing you want to print in a container, then add the container to the print job. This let's you control things like margins and spacing.
private function printData():void
{
var dbPrintJob:FlexPrintJob = new FlexPrintJob();
if(dbPrintJob.start())
{
try
{
dbPrintJob.addObject(this.canvas, FlexPrintJobScaleType.NONE);
}
catch(e:Error)
{
//trace(e);
}
dbPrintJob.send();
}
}
<mx:Canvas id="canvas" width="100%" height="100%">
<mx:Box id="boxPrint" top="10" left="10" bottom="10" right="10" backgroundColor="#FFFFFF">
<mx:Box id="box" width="100%" height="100%" />
</mx:Box>
</mx:Canvas>
Use the canvas to paint your background and space the children into the desired print layout.
I would also suggest giving this a quick read:
Setting size, scale, and orientation
You can try using a simple font such as "Arial", and try if the error still appears, to rule out problems than font.
And avoid using any component if you're having this problem to see if that eliminates the problem.

Enable drag and drop between itemrenderers

I have two tilelists in my mxml application. The items (image and a label) get rendered by an itemrenderer. The functionality I want to achieve: drag image from tilelist #1 and drop it on tilelist #2 (and then a httpservice with sql query will be launched).
How would I tackle this problem? (high level info would suffice).
The main issue I have is that I don't know how to call methods from the main to my itemrenderer. I would like to code the d&d functionality within the renderer but I have no clue how to access watchlist #2 from within the renderer.
Relevant code in main.mxml:
<s:Panel id="panel" width="100%" height="100%" title="Watchlist">
<s:layout>
<s:VerticalLayout paddingBottom="5" paddingLeft="20"
paddingRight="20" paddingTop="5"/>
</s:layout>
<s:Label width="20%" fontSize="17" fontWeight="bold" text="Your watched movies"/>
<mx:TileList id="myWatchedList_tile" height="360" borderVisible="false" width="80%"
columnCount="5" columnWidth="200"
itemRenderer="components.TileListItemRenderer" rowCount="1" rowHeight="360"/>
<s:Label width="20%" fontSize="17" fontWeight="bold" text="Your to watch movies"/>
<mx:TileList id="myToWatchList_tile" height="360" borderVisible="false" width="80%"
columnCount="5" columnWidth="200"
itemRenderer="components.TileListItemRenderer" rowCount="1" rowHeight="360" />
</s:Panel>
The itemrenderer:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
borderVisible="false" horizontalAlign="center" verticalAlign="middle"
xmlns:components="components.*">
<mx:Image source="{data.poster_url}" />
<mx:Label text="{data.movie_title}" height="20" />
</mx:VBox>
You can access methods outside of your item renderer using the outerDocument object. Make sure they are (scope)public methods.
http://www.adobe.com/devnet/flex/articles/itemrenderers_pt1.edu.html
Alternative solution might be to use spark lists instead (with a TileLayout) - then you can easily use drag+drop between lists: http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7cfd.html
..and launch service in response to 'drop' event (event will have reference to dropped image)

Null Object Reference after creating the Object

I have one MXML File as
<objecthandles:ObjectHandles xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" backgroundAlpha="1" xmlns:mx="library://ns.adobe.com/flex/mx"
allowRotate="true" minHeight="25" minWidth="60" height="100" width="200">
<s:BorderContainer id="borderCon" width="100%" height="100%" minHeight="25" minWidth="60"
backgroundAlpha="0" borderVisible="false" borderAlpha="0">
<s:HGroup id="hgText" width="100%" height="100%" gap="0" >
<mx:TextArea id="txtAdd" width="100%" height="100%" color="black"
minHeight="25" minWidth="60" horizontalScrollPolicy="off" verticalScrollPolicy="off" focusOut="txtAddKeyUpHandler(event)"
keyUp="txtAddMirrorKeyUpHandler(event)"
creationComplete="onTextCreationComplete()" />
</s:HGroup>
</s:BorderContainer>
</objecthandles:ObjectHandles>
When ever i create the object of the Fileas
var txtElement:TextElement = new TextElement();
txtElement.txtAdd.text = "Hello";
Then it showing the null object reference that
txtElement.txtAdd seems to be null
Need Perfect Solution?
In the Flex component lifecycle subcomponents will not be created until the parent component is added to the displaylist. Only when the component is added to the displaylist and fully built, will you be able to access its subcomponents. When the component is completely ready for usage, it will dispatch a FlexEvent.CREATION_COMPLETE event.
So do something like this:
var txtElement:TextElement = new TextElement();
txtElement.addEventListener(FlexEvent.CREATION_COMPLETE, initTxtElement);
addElement(txtElement);
private function initTxtElement(event:FlexEvent):void {
txtElement.txtAdd.text = "Hello";
}
Or better yet, since it's a custom component: expose the 'text' property as a property of 'TextElement' and handle the deferred setting of the property internally, so that you can write: txtElement.text = "hello".

Is there an event that is triggered when a component is no longer displayed on the screen?

I have a custom component I've developed that's part of a dashboard.
It does some polling based on a timer that's part of the component.
When the user navigates away from the view that contains this component I would like to stop the timer and hence stop the polling.
I can obviously fire an event when the view's changed and catch it within the component but I was hoping that there might be a way to contain this all within the component.
Is there an event or state change within a component that triggers and even when a component is currently be displayed?
Thanks in advance for any help or suggestions!
Example:
]]>
</mx:Script>
<mx:TabBar x="10" y="10" dataProvider="viewstack1">
</mx:TabBar>
<mx:ViewStack x="0" y="0" id="viewstack1" width="675" height="315">
<mx:Canvas label="View 1" width="100%" height="100%">
<mx:Button x="74" y="69" label="Button 1" width="429" height="185" removedFromStage="removeFromStageEvent()"/>
</mx:Canvas>
<mx:Canvas label="View 2" width="100%" height="100%">
<mx:Button x="74" y="69" label="Button 2" width="429" height="185" color="red"/>
</mx:Canvas>
</mx:ViewStack>
</mx:Application>
removedFromStage is triggered when a component is about to be removed from the stage.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html?filter_flex=4.1&filter_flashplayer=10.1&filter_air=2#REMOVED_FROM_STAGE
Dispatched when a display object is about to be removed from the display list, either directly or through the removal of a sub tree in which the display object is contained. Two methods of the DisplayObjectContainer class generate this event: removeChild() and removeChildAt().
You may be able to use the focus events in combination with a hit test of an invisible sprite that covers the whole stage and/or the view/component visiblity (may need a chain of these).
EDIT:
If you're trying to determine when the user switches between View 1 and View 2 in your ViewStack, you could add an event listener to the viewstack1's change event.
<fx:Script>
<![CDATA[
protected function viewstack1_changeHandler(event:IndexChangedEvent):void
{
// Do Something
}
]]>
</fx:Script>
<mx:TabBar x="10" y="10" dataProvider="viewstack1" />
<mx:ViewStack x="0" y="0" id="viewstack1" width="675" height="315" change="viewstack1_changeHandler(event);">
<mx:Canvas label="View 1" width="100%" height="100%">
<mx:Button x="74" y="69" label="Button 1" width="429" height="185" />
</mx:Canvas>
<mx:Canvas label="View 2" width="100%" height="100%">
<mx:Button x="74" y="69" label="Button 2" width="429" height="185" color="red"/>
</mx:Canvas>
</mx:ViewStack>
If you're using the visible property of your component to determine if it's displayed or not, you can also use the hide event handler in your component.
<local:MyComponent hide="hideHandler(event)">
<fx:Script>
<![CDATA[
protected function hideHandler(event:FlexEvent):void
{
// Do something here.
}
]]>
</fx:Script>
</local:MyComponent>

Flex - Data binding not working

I'm having some troubles with binding data.
I have an application that contains a viewstack of components.
Let's say I have comp1, comp2 and comp3 inside the viewstack.
Each component, has its own data class --> comp1Data.as, comp2Data.as and comp3Data.as.
All values in each component are binded to the corresponding data in it's data object.
A click in a control in comp1 leads to comp3, the same goes for comp2. Clicking a control in comp2 leads to comp3.
When going from comp1 to comp3, comp3Data.as is initialized and and comp3 displays the binded values.
When going from comp2 to comp3, comp3Data.as is also initialized but the binded values are not displayed...
I tried using an initialize but it is not working. And when debugging, the data is right there, but it's is not displaying.
Any help would be really appreciated.
Regards,
BS_C3
Some code ^__^
Main application:
<mx:Application>
<mx:Script>
<![CDATA[
private function order(s:String):void
{
if(s == 'order')
OrderData.instance.state = 'order';
else if(s == 'reporting')
{
OrderData.instance.state = 'reporting';
.
.
.
}
pages.selectedChild = or;
}
]]>
</mx:Script>
<mx:ViewStack id="pages" horizontalCenter="0" verticalCenter="0" width="100%" height="100%">
<components:SearchResult id="sr" width="100%"
order="order('order')"
/>
<components:Reporting id="rp" width="100%"
reportingOrder="order('reporting')"/>
<components:Order id="or" width="100%"
/>
</mx:ViewStack>
</mx:Application>
Both SearchResult and Reporting acces Order.
Order.mxml looks like this:
<mx:Canvas>
<mx:HBox width="100%">
<mx:Box paddingBottom="15" paddingLeft="15" paddingRight="15" paddingTop="15" backgroundColor="#FFFFFF" height="100%">
<components:OrderView id="ov"/>
</mx:Box>
<mx:Spacer width="15"/>
<components:OrderedSDR id="sdr" height="100%"/>
</mx:HBox>
</mx:Canvas>
I'm getting troubles with OrderedSDR.mxml:
<mx:Canvas>
<mx:VBox width="100%" height="100%" paddingBottom="28" paddingLeft="28" paddingRight="28" paddingTop="28" backgroundColor="#FFFFFF">
<mx:HBox width="100%" paddingBottom="6" horizontalAlign="center">
<mx:Canvas>
<mx:Image id="thumbnailBG" source="#Embed(source='assets/Images/SearchResult/BoxBagueOverview.PNG')"/>
<mx:Image id="overview" source="{GlobalData.instance.collection.overview.toString()}"/>
<mx:Label id="thumbnailCarats"
text="{GlobalData.instance.collection.carats.toString() + GlobalData.instance.languageProperties.orderedSDR.imageInfo.toString()}"
styleName="OVLBL"
paddingBottom="5" paddingRight="10"
x="{thumbnailBG.x + thumbnailBG.width - thumbnailCarats.width}"
y="{thumbnailBG.y + thumbnailBG.height - thumbnailCarats.height}"/>
</mx:Canvas>
</mx:HBox>
<mx:VBox id="mainBox" paddingBottom="8" paddingTop="8" verticalGap="6" width="180">
<mx:HBox width="100%" height="13">
<mx:Label width="80"
text="{GlobalData.instance.languageProperties.orderedSDR.product.toString()}"
styleName="OVDataLbl" opaqueBackground="#ECE5E2"/>
<mx:Label text="{SearchResultData.instance.selectedSDR.matnr_fp}" styleName="OVData"/>
</mx:HBox>
<mx:HBox width="100%" height="13">
<mx:Label width="80"
text="{GlobalData.instance.languageProperties.orderedSDR.netPrice.toString()}"
styleName="OVDataLbl" opaqueBackground="#ECE5E2"/>
<mx:Label text="{SearchResultData.instance.selectedSDR.currSymbol + ' ' + SearchResultData.instance.selectedSDR.fNet_price_fp}" styleName="OVData"/>
</mx:HBox>
.
.
.
</mx:VBox>
</mx:VBox>
</mx:Canvas>
Inside mainBox, I've got a list of hbox with the same structure as shown in the first two Hboxes.
Regarding the datasources, these are the following datasources that I'm using, their expected behaviour and their actual behaviour:
#
Embed(source='assets/Images/SearchResult/BoxBagueOverview.PNG --> The image is displayed as it should
GlobalData.instance.collection.overview.toString() --> GlobalData.instance.collection is a XMLList that contains the data for the image with id="overview". I do get the correct source, but the image is not displayed.
GlobalData.instance.languageProperties. ... --> It's a XMLList. The data is shown as it should.
SearchResultData.instance.selectedSDR.matnr_fp --> SearchResultData.instance.selectedSDR is an object that contains all the data that has to be displayed. Matnr_fp is a property of this object. And each hbox inside mainbox have to display a property of the object selectedSDR. The only property that is being displayed is SearchResultData.instance.selectedSDR.currSymbol. The other properties are not displaying even if the datasource is populated.
Hope this will be useful.
Thanks!
Regards,
BS_C3
Looks like nobody's seen anything wrong with the code??
Me neither. But doing other modifications on the code, it finally worked. Still don't know why... >_<

Resources