将动态创建的DataGrid添加为子元素或元素不起作用

时间:2013-12-20 22:18:03

标签: actionscript-3 dynamic datagrid

我正在尝试在AS3中开发一个应用程序。我真正想要实现的是只有一个数据网格,并让它显示三个不同的数据集。 (实际计数将动态变化,因为问题与此无关,所以无关紧要)是的,它必须只是一个数据网格,因为你知道,我需要一个紧凑的接口。

类“Sonuc”有三个属性,它们是构造函数的输入的字符串版本。典型的“Sonuc”对象就是这样的。

var sonuc1:Sonuc = new Sonuc(1,1,false);
//sonuc1.num = "1"
//sonuc1.type = "1"
//sonuc1.isTrue = "No"

我告诉你关于“Sonuc”课程的原因是我希望你知道课程不是太复杂。 x.mxml是测试mxml,我只加载该类用于测试目的。

这是我到目前为止所编码的

public class ResultInterface extends UIComponent
    {

    private const desiredWidth:int = 250;
    private const desiredHeight:int = 150;

    private const sonuc1:Sonuc = new Sonuc(1,1,false);

    public var tablo:DataGrid = new DataGrid();
    public var kolonArray:Array = new Array ();
    public var sonucArray:Array = new Array ();

    public var currentIndex:int = new int ();
    public var prevButon:Button = new Button();
    public var nextButon:Button = new Button();


    public function ResultInterface():void
    {

        currentIndex = 0;

        super();

        tablo = new DataGrid();

        width=desiredWidth+40;
        height=desiredHeight+60;

        this.tablo.width = desiredWidth;
        this.tablo.height = desiredHeight;
        this.tablo.x = 20;
        this.tablo.y = 40;

        prevButon.x = 10;
        prevButon.y = genislik/2 - 10;
        prevButon.width = 
        prevButon.height = 10; 

        nextButon.x = genislik +20;
        nextButon.y = genislik/2 -10;
        nextButon.width =
        nextButon.height = 10;

        var referansColl:ArrayCollection = new ArrayCollection();

        sonucArray.push(referansColl);

        tablo.dataProvider = sonucArray[currentIndex];

        var sampleCol:DataGridColumn = new DataGridColumn();
        sampleCol.dataField = "num";
        sampleCol.headerText = "Number";

        var sampleCol2:DataGridColumn = new DataGridColumn();
        sampleCol2.dataField = "type";
        sampleCol2.headerText = "Type";

        var sampleCol3:DataGridColumn = new DataGridColumn();
        sampleCol3.dataField = "isTrue";
        sampleCol3.headerText = "Is it true?";

        kolonArray.push(sampleCol,sampleCol2,sampleCol3);
        tablo.columns = kolonArray;


        this.addElement(tablo); //**** this is the problematic line
        this.addChild(oncekiButon);
        this.addChild(sonrakiButon);

    }

    public function getNewSonuc(incoming:Sonuc):void
    {
        sonucArray[currentIndex].addItem(incoming);
    }

    public function newTablo():void
    {
        var newTablo:ArrayCollection = new ArrayCollection();
        currentIndex = sonucArray.push(newTablo);
    }

    public function prev(evt:Event):void //I haven't written event listeners yet
    {
        if(currentIndex > 0)
        currentIndex--;

            nextButon.enabled = true;

        if(currentIndex == 0)
        prevButon.enabled = false;
    }

    public function birSonrakine(evt:Event):void
    {
        if(currentIndex < sonucArray.length)
        currentIndex++;

        prevButon.enabled = true;

        if(currentIndex == sonucArray.length)
        nextButon.enabled = false;
    }

在这个版本中,我得到一个语法错误“调用可能未定义的方法addElement” 我也尝试将基类设为“Sprite”和“Canvas”

当我使用addChild而不是addElement时,我得到运行时错误“addChild不可用于此类”

当我刚评论出有问题的线路时,所有内容都完全加载了但是数据网格本身。

请注意,在将某些数据(Sonuc)发送到datagrid之前会发生错误。

当我尝试使用canvas和addelement时,我得到了“无法使用一些奇怪的函数,类和包来访问null对象引用的属性或方法”。

1009:无法访问空对象引用的属性或方法。

at mx.styles::StyleProtoChain$/initProtoChainForUIComponentStyleName()[E:\dev\4.y\frameworks\projects\framework\src\mx\styles\StyleProtoChain.as:358]
at mx.styles::StyleProtoChain$/initProtoChain()[E:\dev\4.y\frameworks\projects\framework\src\mx\styles\StyleProtoChain.as:171]
at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::initProtoChain()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:10926]
at mx.core::UIComponent/regenerateStyleCache()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:10989]
at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::addingChild()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7465]
at mx.core::UIComponent/addChild()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7162]
at mx.controls.listClasses::ListBase/createChildren()[E:\dev\4.y\frameworks\projects\mx\src\mx\controls\listClasses\ListBase.as:3772]
at mx.controls::DataGrid/createChildren()[E:\dev\4.y\frameworks\projects\mx\src\mx\controls\DataGrid.as:1143]
at mx.core::UIComponent/initialize()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7634]
at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::childAdded()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7495]
at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::childAdded()[E:\dev\4.y\frameworks\projects\mx\src\mx\core\Container.as:3974]
at mx.core::Container/addChildAt()[E:\dev\4.y\frameworks\projects\mx\src\mx\core\Container.as:2618]
at mx.core::Container/addChild()[E:\dev\4.y\frameworks\projects\mx\src\mx\core\Container.as:2534]
at mx.core::Container/addElement()[E:\dev\4.y\frameworks\projects\mx\src\mx\core\Container.as:2981]
at genel.siniflar::ResultInterfaceArayuz()[C:\Users\Ege\Adobe Flash Builder\brainswift2\src\genel\siniflar\ResultInterface.as:95]
at x()[C:\Users\Ege\Adobe Flash Builder\brainswift2\src\x.mxml:27]
at _x_mx_managers_SystemManager/create()[_x_mx_managers_SystemManager.as:54]
at mx.managers.systemClasses::ChildManager/initializeTopLevelWindow()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\systemClasses\ChildManager.as:311]
at mx.managers::SystemManager/initializeTopLevelWindow()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\SystemManager.as:3057]
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::kickOff()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\SystemManager.as:2843]
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::preloader_completeHandler()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\SystemManager.as:2723]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.preloaders::Preloader/timerHandler()[E:\dev\4.y\frameworks\projects\framework\src\mx\preloaders\Preloader.as:542]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

我真的需要你们的帮助,请尽快回答。

1 个答案:

答案 0 :(得分:0)

好吧,我尝试从构造函数外部调用“addElement”,它起作用了。但是我不知道究竟是什么导致了这个错误,我知道我的解决方案不合法。我想学习妥善解决这个问题。

更改为resultInterface.as

    // this.addElement(tablo); **** this is the problematic line, we have commented it out
    this.addChild(oncekiButon);
    this.addChild(sonrakiButon);

更改为x.mxml

public function onCreationComplete(evt:Event):void
{
showResult.addElement(showResult.tablo);
addElement(showResult);
}