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
New Developer
bdeneau
Posts: 34
Registered: ‎03-23-2011
My Device: Bold 9700
Accepted Solution

Problem setting focus - stage is null?

I'm trying to programmatically set the focus of a textinput however the debugger is throwing me this error:

 

Cannot access a property or method of a null object reference.

 

I'm trying to set the focus with this line:

 

stage.focus = input;

 

For reference here is a bit of the code:

 

package views
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.geom.Matrix;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import qnx.input.IMFConnection;
	import qnx.ui.buttons.LabelButton;
	import qnx.ui.core.UIComponent;
	import qnx.ui.display.Image;
	import qnx.ui.text.Label;
	import qnx.ui.text.TextInput;

	public class RenamePopupContainer extends UIComponent
	{
		private var input:TextInput;
		private var file:String;
		
		public function RenamePopupContainer(_file:String)
		{
			super();
			file = _file;
			setSize(1024, 600);
			setPosition(0, 0);
			createChildren();
			
			IMFConnection.imfConnection.showInput();
			stage.focus = input;
		}
		
		private function createChildren():void
		{
			var labelFormat:TextFormat = new TextFormat();
			labelFormat.color = 0xFFFFFF;
			labelFormat.size = 28;
			labelFormat.align = TextFormatAlign.CENTER;
			
			var mat:Matrix = new Matrix();
			mat.createGradientBox(400, 1);
			
			var line1:Shape = new Shape();
			line1.x = 312;
			line1.y = 92;
			line1.graphics.beginGradientFill(GradientType.LINEAR, [0xeeeeee, 0xeeeeee, 0xeeeeee], [0, 1, 0], [0, 127, 255], mat);
			line1.graphics.drawRect(0, 0, 400, 1);
			line1.graphics.endFill();
			addChild(line1);
			
			var label:Label = new Label();
			label.text = "Rename to?";
			label.setSize(450, 40);
			label.setPosition(287, 43);
			label.format = labelFormat;
			addChild(label);
			
			input = new TextInput();
			input.setSize(380, 40);
			input.setPosition(322, 120);
			input.text = file;
			addChild(input);
			
			var line2:Shape = new Shape();
			line2.x = 312;
			line2.y = 190;
			line2.graphics.beginGradientFill(GradientType.LINEAR, [0xeeeeee, 0xeeeeee, 0xeeeeee], [0, 1, 0], [0, 127, 255], mat);
			line2.graphics.drawRect(0, 0, 400, 1);
			line2.graphics.endFill();
			addChild(line2);
			
			var btnOk:LabelButton = new LabelButton();
			btnOk.setSize(120, 40);
			btnOk.setPosition(384, 210);
			btnOk.label = "Ok";
			btnOk.addEventListener(MouseEvent.CLICK, ok);
			addChild(btnOk);
			
			var btnCancel:LabelButton = new LabelButton();
			btnCancel.setSize(120, 40);
			btnCancel.setPosition(519, 210);
			btnCancel.label = "Cancel";
			btnCancel.addEventListener(MouseEvent.CLICK, cancel);
			addChild(btnCancel);
		}
	}
}

 

Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Problem setting focus - stage is null?

It's a fairly common problem: a DisplayObject's stage property is null until it has been added into the display list.  There are a few solutions, but the simplest for you might be this one, if this object will only ever be added to the display list once.

 

// in place of your existing stage.focus line
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

...

private function onAddedToStage(e:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    stage.focus = input;
}

 


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Developer
bdeneau
Posts: 34
Registered: ‎03-23-2011
My Device: Bold 9700

Re: Problem setting focus - stage is null?

THanks for the reply, but it only sort of worked.  If I set the focus from within the container, it doesn't actually set the focus, it creates a yellow box around the textinput, but doesn't automatically allow heyboard input.

 

If I make the textinput public and set the focus from outside the container, in the calling class, it properly focuses.

 

Is there a way, or something I'm missing, to set the focus from in the container?