1

Maybe a simple question but I'm having alot of trouble making a button change the view of a Flex blackberry playbook app. I am coding it entirely in actionscript, no MXML.

myButton.addEventListener(MouseEvent.CLICK, doSomethingOnClick);

private function doSomethingOnClick(e:MouseEvent):void {
    navigator.pushView(view.Login, "testdata");
}

When I try this I get:

1120: Access of undefined property navigator.

Which is weird as it works in a MXML file. How do I change views in actionscript?

Thanks Phil

EDIT: Cheer J_A_X, but now i have:

navigator = new ViewNavigator();
navigator.pushView(net.airpoint.assessments.view.Login, " ");

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Apologies, as I realise this is really simple stuff but it just isnt clicking!

Update 2 *Assessments.as*

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    import net.airpoint.assessments.view.*;

    import qnx.ui.core.Container;
    import qnx.ui.core.ContainerAlign;
    import qnx.ui.core.ContainerFlow;
    import qnx.ui.core.Containment;
    import qnx.ui.text.Label;

    import spark.components.ViewNavigator;

    [SWF(height="600", width="1024", frameRate="30", backgroundColor="#FFFFFF")]

    /* Main Layout */

    public class Assessments extends Sprite
    {
        //containers 
        private var main:Container;
        private var menu:Container
        private var firstLabel:Label;
        private var navigator:ViewNavigator;


        public function Assessments()
        {
            initializeUI();
        }

        private function initializeUI():void
        {
            main = new Container();
            main.padding = Vector.<Number>([20,20,20,20]);
            main.flow = ContainerFlow.HORIZONTAL;
            main.debugColor = 0xFFCC00;

            firstLabel = new Label();
            firstLabel.text = "First label";
            firstLabel.size=35;

            main.addChild(firstLabel);

            addChild(main);

            navigator = new ViewNavigator();
            navigator.pushView(Login, " ");

        }
    }
}

Login.as

package net.airpoint.assessments.view
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    import qnx.ui.buttons.Button;
    import qnx.ui.core.Container;
    import qnx.ui.text.Label;
    import qnx.ui.text.TextInput;

    import spark.components.View;

    public class Login extends View
    {

        private var usernameLabel:Label;
        public function Login()
        {
            initializeUI();
        }

        public function initializeUI():void
        {
            usernameLabel.text = "test";

            this.addChild(usernameLabel);
        }               
    }
}
p_mcp
  • 2,643
  • 8
  • 36
  • 75
  • 1
    Do you actually have the navigator added? Says here that you don't. – J_A_X Apr 12 '11 at 14:44
  • Oh dear! lol. As it worked without instantiating in the MXML I was under the inpression it was some sort of language keyboard. However, now i have: navigator = new ViewNavigator(); navigator.pushView(net.airpoint.assessments.view.Login, " "); and I get...: TypeError: Error #1009: Cannot access a property or method of a null object reference. – p_mcp Apr 12 '11 at 15:08
  • Is this a pure actionscript project? Because Flex components works differently since AS projects are Sprite based. Also, you don't need to add the full path, just `Login` would work as long as it's imported. And lastly, you want to add the ViewNavigator to the displaylist by doing `addElement(navigator)`. – J_A_X Apr 12 '11 at 15:12
  • Ye its a pure actionscript project for the blackberry playbook as they recommend not using MXML. Still not working unfortunately, Ill add my full code to the post... thanks again – p_mcp Apr 12 '11 at 15:16
  • I should also say, the code is purely test, it doesnt do much, jsut trying to get a grip with full AS applications in FLex – p_mcp Apr 12 '11 at 15:19
  • 1
    You can't use Flex components in a pure AS project without hacking it together, so anything starting with the 'spark' namespace is off limit. Again, you can't add a Flex component to a Sprite, so you're SOL unless you use MXML. If you're going to use Flex components, might as well use MXML, it's much better than AS when it comes to layout out your UI. – J_A_X Apr 12 '11 at 15:23
  • Ahh sorry Ive gotten confused, the project is a "Flex Mobile Project" using only actionscript as blackberry recommend against mxml... So I still have access to spark and flex components, I get no compilation errors – p_mcp Apr 12 '11 at 15:33

2 Answers2

0

Something isn't right. If it's a Flex Mobile Project, you need an Application at the top level (you know, like how Flash Builder created the project with an mxml file). Either you create an actionscript file that extends Application as mentioned here or you just use an mxml file for the root component.

However, your argument to 'not use mxml' is redundant if you're using Flex components. If you're using Flex components, you're using mxml no matter what, so there's no performance increase. If anything, RIM is recommending to use AS only because their SDK is AS only (which is idiotic anyways). You could always add their UI component through AS in mxml files.

So really, the point is moot and you should just use mxml anyways since it's better than straight AS for UI layout and skinning. Either that or go Pure AS with no Flex components.

Community
  • 1
  • 1
J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • After all that I think Im just going to go for MXML, its so much easier and faster... thanks again – p_mcp Apr 12 '11 at 16:13
0

I think using Sprite is ok in an ActionScript project.

But if you're using ActionScript instead of Flex just because of the QNX components, then you could switch your project to Flex and follow these instructions to use the QNX components there: Using qnx.ui.picker.Picker in mobile Flex Hero project for Blackberry Playbook

Community
  • 1
  • 1
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416