无法使用SplitViewNavigator容器

时间:2012-10-24 15:09:44

标签: sqlite actionscript-3 flex flex4.6

我想使用SplitViewNavigator容器创建一个App,其中包含左视图中的城市列表和右视图中城市的详细信息,右视图中有一个文本输入,通过我获取城市名称并存储在SQLite数据库中,并且该名称应该从SQLite数据库的左侧视图中添加到列表中我开始使用Main.mxml中的流动代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                        xmlns:s="library://ns.adobe.com/flex/spark" 
                        applicationDPI="160"
                        initialize="application1_initializeHandler(event)">
<fx:Script>
    <![CDATA[
        import model.DataModel;

        import mx.events.FlexEvent;
        import valueobject.CityValueObject;
        import utillities.CityUtils;

        public var sqlConnection:SQLConnection;
        protected var statement:SQLStatement;
        protected function application1_initializeHandler(event:FlexEvent):void
        {
            sqlConnection = new SQLConnection();
            sqlConnection.open(File.applicationStorageDirectory.resolvePath("cityDB.db"), SQLMode.CREATE);
            statement.sqlConnection = sqlConnection; // Here error occurs saying that Error #1009: Cannot access a property or method of a null object reference.
            statement.text = "CREATE TABLE IF NOT EXISTS CITYNAME (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "nameofcity TEXT)";
            statement.execute();
            DataModel.getInstance().connection = sqlConnection;

            CityUtils.getAllCities();
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:SplitViewNavigator id="svn" width="100%" height="100%">
    <s:ViewNavigator width="30%" height="100%" id="list_of_cities" firstView="views.ListOfCities"/>
    <s:ViewNavigator width="70%" height="100%" id="display_contents" firstView="views.DisplayContents"/>
</s:SplitViewNavigator>

我的model.DataModel是一个动作脚本类:

    package model
    {
    import flash.data.SQLConnection;

    import mx.collections.ArrayCollection;

    [Bindable]
    public class DataModel
    {
        public var connection:SQLConnection;
        public var cityList:ArrayCollection = new ArrayCollection();
        public var logs:String="Application Logs........\n";

        public static var _instance:DataModel;

        public function DataModel()
        {

        }

        public static function getInstance():DataModel
        {
            if(_instance == null)
            {
                _instance = new DataModel();
            }
            return _instance;
        }
    }
}

我的valueobject.CityValueObject类是:

package valueobject
{
[Bindable]
public class CityValueObject
{
    public var id:uint;
    public var nameofcity:String;
}}

我的uttillities.CityUtils类是::

package utillities
{
import flash.data.SQLResult;
import flash.data.SQLStatement;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.ByteArray;

import model.DataModel;

import mx.collections.Sort;
import mx.collections.SortField;

import valueobject.CityValueObject;


public class CityUtils
{
    public static function getAllCities():void
    {
        var contactListStatement:SQLStatement = new SQLStatement();
        contactListStatement.sqlConnection = DataModel.getInstance().connection;
        contactListStatement.text = "SELECT * FROM CITYNAME";
        contactListStatement.execute();
        var result:SQLResult = contactListStatement.getResult();
        if( result.data!=null)
        {
            DataModel.getInstance().cityList.removeAll();

            for(var count:uint=0;count<result.data.length;count++)
            {
                var cityVO:CityValueObject = new CityValueObject();
                cityVO.id = result.data[count].id;
                cityVO.nameofcity = result.data[count].city;                    
                DataModel.getInstance().cityList.addItem(cityVO);
            }
        }
        sortData();     
    }

    public static function sortData():void
    {
        var dataSortField:SortField = new SortField();
        dataSortField.name = "cityName";
        dataSortField.numeric = false;

        /* Create the Sort object and add the SortField object created earlier to the array of fields to sort on. */
        var numericDataSort:Sort = new Sort();
        numericDataSort.fields = [dataSortField];

        /* Set the ArrayCollection object's sort property to our custom sort, and refresh the ArrayCollection. */
        DataModel.getInstance().cityList.sort = numericDataSort;
        DataModel.getInstance().cityList.refresh();
    }

    public static function updateLog(newLog:String):void
    {
        DataModel.getInstance().logs += new Date().time+" :-> "+newLog+"\n";
    }
}

}

我的左边包含城市列表:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="Cities"
    >
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[
        import model.DataModel;

        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;
        import mx.events.IndexChangedEvent;

        import spark.components.SplitViewNavigator;
        import spark.components.ViewNavigator;
        import spark.transitions.ViewTransitionBase;
        protected function myList_changeHandler():void {
            // Create a reference to the SplitViewNavigator.
            var splitNavigator:SplitViewNavigator = navigator.parentNavigator as SplitViewNavigator;

            // Create a reference to the ViewNavigator for the Detail frame.
            var detailNavigator:ViewNavigator = splitNavigator.getViewNavigatorAt(1) as ViewNavigator;

            detailNavigator.transitionsEnabled = false;

            // Change the view of the Detail frame based on the selected List item.
            detailNavigator.pushView(DisplayContents, list_of_cities.selectedItem);             
        }           
    ]]>
</fx:Script>
<s:VGroup width="100%" height="100%">
    <s:List id="list_of_cities" height="100%" width="100%" change="myList_changeHandler();" 
            dataProvider="{DataModel.getInstance().cityList}" labelField="nameofcity">

    </s:List>       
</s:VGroup>

最后我的关于城市的显示细节就像这样:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="Detail About City"
    >
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->

</fx:Declarations>
<s:actionContent>
    <s:CalloutButton id="add_call_out_button" label="Add City" verticalPosition="after" 
                     icon="@Embed('assets/add.png')" calloutDestructionPolicy="never">
        <!-- layout the callout content here -->
        <s:calloutLayout>
            <s:VerticalLayout paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10" horizontalAlign="center" gap="5"/>
        </s:calloutLayout>
        <s:calloutContent>              
            <s:TextInput id="city_name_input" prompt="Enter City Name" text="Sydney"/>
            <s:HGroup gap="40">
                <s:Button id="add_city_name" label="Add City" width="150" height="40" click="add_city_name_clickHandler()"/>
                <s:CheckBox id="preferred_cbox" label="Preferred" height="40" />
            </s:HGroup>

        </s:calloutContent>
    </s:CalloutButton>
    <s:Button id="remove_city_name" label="Remove" width="120" height="40"
              click="remove_city_name_clickHandler()" icon="@Embed('assets/delete.png')"/>

</s:actionContent>

<s:Label id="nameSomeThing" text="{data.Description}"/>

<fx:Script>
    <![CDATA[
        import model.DataModel;

        import spark.components.SplitViewNavigator;
        import spark.components.ViewNavigator; 

        protected function add_city_name_clickHandler():void
        {
            var sqlStatement:SQLStatement = new SQLStatement();
            sqlStatement.sqlConnection = DataModel.getInstance().connection;
            sqlStatement.text = "INSERT INTO CITYNAME (nameofcity)" +
                                "VALUES(:nameofcity)";
            sqlStatement.parameters[":nameofcity"] = city_name_input.text;
            sqlStatement.execute();

            var splitNavigator:SplitViewNavigator = navigator.parentNavigator as SplitViewNavigator;

            // Create a reference to the ViewNavigator for the Detail frame.
            var detailNavigator:ViewNavigator = splitNavigator.getViewNavigatorAt(1) as ViewNavigator;

            detailNavigator.transitionsEnabled = false;

            // Change the view of the Detail frame based on the selected List item.
            detailNavigator.popToFirstView();
        }

        protected function remove_city_name_clickHandler():void
        {
            // TODO Auto-generated method stub

        }

    ]]>
</fx:Script>

上面的视图(显示细节)仍在开发中,但在此阶段我试图通过从城市名称输入文本输入中获取名称来将城市名称添加到城市列表,但是在:

statement.sqlConnection = sqlConnection; // Here error occurs saying that Error #1009: Cannot access a property or method of a null object reference.

我发现了这个错误而无法继续。

任何人都可以通过上面的代码givien给我解决我的问题的方法或建议我通过此应用程序满足我的需求的其他方式提前感谢...

1 个答案:

答案 0 :(得分:0)

正如错误消息所示,statement为空。

我没有看到任何会初始化它的代码。 你需要:

statement = new SQLStament();

(我认为这个变量不需要超出application1_initializeHandler函数的原因。)