使用 TileList 作为参数调用 addChild 时,ActionScript 抛出错误 #1009
准确的说是这个错误。
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at fl.containers::BaseScrollPane/drawBackground()
at fl.controls::TileList/draw()
at fl.core::UIComponent/callLaterDispatcher()
现在我已经尝试了此页面中的几个 Adobe 自己的示例, http ://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/TileList.html,它们也都抛出这个错误。
该错误是由作为 addChild 函数参数的 TileList 实例触发的。
这是我的包,当我将 displayComponent 更改为列表时,它可以正常工作。
package com.pennstate {
import fl.data.DataProvider;
import flash.display.MovieClip;
import flash.display.DisplayObjectContainer;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextFormat;
import flash.xml.XMLDocument;
import com.adobe.serialization.json.JSON;
import fl.controls.List;
import fl.controls.TileList;
public class Sign {
public var displayComponent:TileList;
public var url:String;
public var provider:DataProvider;
public var mc:MovieClip;
public var container:DisplayObjectContainer;
public function Sign( url:String, container ) {
this.container = container;
this.displayComponent = new TileList();
this.mc = new MovieClip();
this.url = url;
this.provider = new DataProvider();
_componentSetup();
loadJson();
_componentFormat();
}
private function _componentSetup() {
displayComponent.labelFunction = getLabelFieldContent;
displayComponent.sourceFunction = getSourceFieldContent;
displayComponent.dataProvider = provider;
displayComponent.selectable = false;
displayComponent.setStyle("contentPadding", 5);
displayComponent.setSize(1720,770);
displayComponent.move(100,200);
displayComponent.rowHeight = 190;
trace('End setup');
}
private function _componentFormat() {
var listTextFormat:TextFormat = new TextFormat();
listTextFormat.font = "Arial";
listTextFormat.color = 0x000000;
listTextFormat.bold = true;
listTextFormat.size = 48;
displayComponent.setRendererStyle("textFormat", listTextFormat);
trace('End formatting');
}
function loadJson():void {
var jsonLoader:URLLoader = new URLLoader();
jsonLoader.addEventListener(Event.COMPLETE, onJsonComplete);
jsonLoader.load( new URLRequest( url ) );
}
function onJsonComplete(e:Event):void {
trace('Loading finished.');
var jsonData:String = e.target.data;
trace(jsonData + "\n");
var decodedData = JSON.decode(jsonData, false);
for (var index in decodedData.rows) {
provider.addItem({title: decodedData.rows[index].node.title, result: decodedData.rows[index].node.Result});
trace(index+" => "+decodedData.rows[index].node.title);
trace(index+" => "+decodedData.rows[index].node.Result);
}
container.addChild(displayComponent);
}
function getLabelFieldContent(item:Object):String {
return new XMLDocument(item.title + "\n" + item.result).firstChild.nodeValue;
}
function getSourceFieldContent(item:Object):String {
return item.result;
}
}
}
To be exact this is the error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at fl.containers::BaseScrollPane/drawBackground()
at fl.controls::TileList/draw()
at fl.core::UIComponent/callLaterDispatcher()
Now I've tried several of Adobe's own examples from this page, http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/TileList.html, and they all throw this error as well.
The error is triggered by the TileList instance being the argument of the addChild function.
Here's my package, which works fine when I change the displayComponent is be a List.
package com.pennstate {
import fl.data.DataProvider;
import flash.display.MovieClip;
import flash.display.DisplayObjectContainer;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextFormat;
import flash.xml.XMLDocument;
import com.adobe.serialization.json.JSON;
import fl.controls.List;
import fl.controls.TileList;
public class Sign {
public var displayComponent:TileList;
public var url:String;
public var provider:DataProvider;
public var mc:MovieClip;
public var container:DisplayObjectContainer;
public function Sign( url:String, container ) {
this.container = container;
this.displayComponent = new TileList();
this.mc = new MovieClip();
this.url = url;
this.provider = new DataProvider();
_componentSetup();
loadJson();
_componentFormat();
}
private function _componentSetup() {
displayComponent.labelFunction = getLabelFieldContent;
displayComponent.sourceFunction = getSourceFieldContent;
displayComponent.dataProvider = provider;
displayComponent.selectable = false;
displayComponent.setStyle("contentPadding", 5);
displayComponent.setSize(1720,770);
displayComponent.move(100,200);
displayComponent.rowHeight = 190;
trace('End setup');
}
private function _componentFormat() {
var listTextFormat:TextFormat = new TextFormat();
listTextFormat.font = "Arial";
listTextFormat.color = 0x000000;
listTextFormat.bold = true;
listTextFormat.size = 48;
displayComponent.setRendererStyle("textFormat", listTextFormat);
trace('End formatting');
}
function loadJson():void {
var jsonLoader:URLLoader = new URLLoader();
jsonLoader.addEventListener(Event.COMPLETE, onJsonComplete);
jsonLoader.load( new URLRequest( url ) );
}
function onJsonComplete(e:Event):void {
trace('Loading finished.');
var jsonData:String = e.target.data;
trace(jsonData + "\n");
var decodedData = JSON.decode(jsonData, false);
for (var index in decodedData.rows) {
provider.addItem({title: decodedData.rows[index].node.title, result: decodedData.rows[index].node.Result});
trace(index+" => "+decodedData.rows[index].node.title);
trace(index+" => "+decodedData.rows[index].node.Result);
}
container.addChild(displayComponent);
}
function getLabelFieldContent(item:Object):String {
return new XMLDocument(item.title + "\n" + item.result).firstChild.nodeValue;
}
function getSourceFieldContent(item:Object):String {
return item.result;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有在构造函数中为您的
container
参数指定类型,即: UIComponent这加上它与您的成员变量同名的事实可能是原因。
You have not given your
container
agrument in the constructor a type i.e: UIComponentThis coupled with the fact that its the same name as your member variable is probably the cause.
我必须使用 Flash CS4 GUI 将实际的 TileList 组件从组件菜单拖到舞台上才能消除此错误。
奇怪的部分是我拖到舞台上的组件不是我在代码中使用的组件。我在代码中动态创建的组件现在可以工作了。
我什至删除了添加到舞台的 TileList 组件,但它仍然有效。这对我来说听起来像是一个错误。
I had to drag an actual TileList component from the Component Menu onto the Stage using the Flash CS4 GUI to make this error go away.
The weird part is the component that I dragged onto the Stage isn't the component I use in the code. The component I created dynamically in the code now works though.
I even deleted the TileList component that I added to the Stage and it still works. This sounds like a bug to me.