Focus Management with the Flex Component Kit for Flash

5 11 2009

At work today we found a bug in the Flex Component Kit for flash. The basic synopsis: when you create a “component” in flash using the CS3 component kit it automatically causes you to extend a base class known as mx.flash.UIMovieClip. This class is very useful, as according to it’s own documentation it allows you:

The UIMovieClip class implements the interfaces necessary for a Flash component to be used like a normal Flex component. Therefore, a subclass of UIMovieClip can be used as a child of a Flex container or as a skin, and it can respond to events, define view states and transitions, and work with effects in the same way as can any Flex component.

I think you can see the use for that. You can develop rudimentary UI elements in flash (which it was designed for), and use them in a flex (MXML) application. In concept this is awesome, and it usually works. However, we were plagued by a seeming randomly occuring bug for quite a period of time. As with all such bugs, it’s not really random, we just hadn’t found the cause. We broke down and finally started rooting around for the cause. We eventually found it, and want to help prevent everyone else from experiencing it, or at least solve it once you do experience it.

First, the error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mx.flash::UIMovieClip/removeFocusEventListeners()[E:\dev\trunk\frameworks\proje cts\flash-integration\src\mx\flash\UIMovieClip.as:2466]
at mx.flash::UIMovieClip/focusOutHandler()[E:\dev\trunk\frameworks\projects\flash- integration\src\mx\flash\UIMovieClip.as:2509]
at flash.display::Stage/set focus()
at fl.core::UIComponent/setFocus()
at fl.managers::FocusManager/setFocus()
at fl.managers::FocusManager/mouseDownHandler()

Next, the symptoms:

This error is a common one, the null object reference. Combine this with the referenced FocusManager and it is the kind of thing out of nightmares for any experienced ActionScript Developer.
We are creating basic visual assets in Flash, and some of them are using Flash components. At first we assumed this was the cause, but it turns out that it wasn’t. Frequently we would click somewhere outside the app (to loose focus) or click on another component (transfer focus) and that error would come up so often our app was useless. Even just going to the debugger would cause it.

Next, the cause:

In the end we discoverd the error. We would remove an asset that we had created in flash from the stage. This *does not* trigger the focus to be transferred. (it probably should, but it is what it is at the moment.) There in lies our problem. A component would be off stage, but the focus manager wouldn’t know that. As soon as we would interact with something else and it would attempt to transfer focus *BOOM* the error would occur. This would happen because the asset no longer had a stage from which to remove the even listeners that UIMovieClip is automatically trying to do in it’s focusOutHandler function.

Last, the workaround:

To workaround his issue isn’t incredibly complex thankfully. You just have to remember to transfer the focus to something else, such as the mxml stage, or something else before removing any flash components from the stage. Simple right? Yes, but highly frustrating when you forget to do it. Danwize, one of my co-workers filed a bug on bugs.adobe.com. You can vote, follow or, whatever, here: bug# SDK-24015.

Advertisements

Actions

Information

2 responses

11 03 2010
Yoav Katz

At last a solution to this problem…
You saved me a lot of time and pain, thank you!

11 03 2010
captaincode

I’m glad I could help!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: