2013-06-26 74 views
1

我想爲使用Adobe Flash Builder 4.6的Android構建一個簡單的手機電話簿應用程序,但是當我試圖運行應用程序調試器時,不斷給我一些錯誤..我發佈的所有代碼在我的項目,請看一看:(在Flash Builder移動應用程序中的數據庫(動作腳本3.0)

dbHandler類:

package ActionScript 
{ 
import air.net.URLMonitor; 

import flash.data.SQLConnection; 
import flash.data.SQLResult; 
import flash.data.SQLStatement; 
import flash.events.SQLErrorEvent; 
import flash.events.SQLEvent; 
import flash.filesystem.File; 
import flash.net.URLLoader; 
import flash.net.URLRequest; 

import mx.collections.ArrayCollection; 

import views.NewContactView; 
import views.PhoneBookHomeView; 






public class dbHandler 
{ 

    private var urlMonitor:URLMonitor; 
    private var urlLoader:URLLoader; 
    private var urlRequest:URLRequest; 
    private var sqlConnection:SQLConnection; 
    private var db:File; 
    private var allContacts:ArrayCollection = new ArrayCollection(); 

    public function dbHandler() 
    { 
     trace("dbHandler object was created"); 
     openDb(); 
    } 

    private function openDb():void 
    { 
     db = File.applicationStorageDirectory.resolvePath("DataBase.db"); 

     sqlConnection = new SQLConnection(); 
     sqlConnection.addEventListener(SQLErrorEvent.ERROR, onSQLError); 

     if(db.exists) 
     { 
      trace("DataBase exists"); 
      sqlConnection.addEventListener(SQLEvent.OPEN, onOpenDb); 

     } else { 
      trace("DataBase does not exist"); 
      sqlConnection.addEventListener(SQLEvent.OPEN, onCreateDb); 

     } 

     sqlConnection.openAsync(db); 
    } 

    private function onCreateDb(event:SQLEvent):void 
    { 
     trace("The database was created"); 
     createTable(); 
    } 

    private function onOpenDb(e:SQLEvent):void 
    {  
     trace("The database was opened"); 

     if(SQLConnection(e.target).connected) 
     { 
      trace("SQL Connection is connected"); 
     }   

    } 

    private function onSQLError(e:SQLErrorEvent):void 
    {  
     var err:String = "-> Error id :" 
      + e.error.errorID 
      + " \ -> Details:" 
      + e.error.details; 

     trace ("SQL Error :" + err + "->Error"); 
    } 

    private function createTable():void 
    { 
     var sqlText:String = "CREATE TABLE IF NOT EXISTS contacts (_id INTEGER PRIMARY KEY AUTOINCREMENT," + 
     " name TEXT,value TEXT);"; 
     trace("Creating table by : " + sqlText); 
     var createTableSQL:SQLStatement = new SQLStatement(); 
     createTableSQL.addEventListener(SQLEvent.RESULT, onTable); 
     createTableSQL.addEventListener(SQLErrorEvent.ERROR, onSQLError); 
     createTableSQL.sqlConnection = sqlConnection; 
     createTableSQL.text = sqlText; 
     createTableSQL.execute(); 
    } 

    private function onTable(e:SQLEvent):void 
    { 
     trace("The table was created");  
    } 

    public function deleteContact(id:int):void 
    {  
     var sqlText:String = "DELETE FROM contacts WHERE _id = " + id; 
     trace("Deleting item (id="+id+") by:"+sqlText); 
     var deleteTeamSQL:SQLStatement = new SQLStatement(); 
     deleteTeamSQL.sqlConnection = sqlConnection; 
     deleteTeamSQL.addEventListener(SQLEvent.RESULT, onDelete); 
     deleteTeamSQL.addEventListener(SQLErrorEvent.ERROR, onSQLError); 
     deleteTeamSQL.text = sqlText; 
     deleteTeamSQL.execute();     
    } 

    private function onDelete(e:SQLEvent):void 
    { 
     trace("item was deleted"); 
    } 

    public function getAll():ArrayCollection 
    { 
     var sqlText:String = "SELECT * FROM contacts"; 
     trace("Geting all contacts by: "+sqlText); 
     var getAllSQL:SQLStatement = new SQLStatement(); 
     getAllSQL.sqlConnection = sqlConnection; 
     getAllSQL.addEventListener(SQLEvent.RESULT, ongetAll); 
     getAllSQL.addEventListener(SQLErrorEvent.ERROR, onSQLError); 
     getAllSQL.text = sqlText; 
     getAllSQL.execute(); 
       return allContacts; 
    } 

    private function ongetAll(e:SQLEvent):void 
    { 
     trace("All contacts were got"); 
      var selectSQL:SQLStatement = SQLStatement(e.target); 
      var result:SQLResult = selectSQL.getResult(); 
      if(result.complete) 
      { 
       if(result.data) 
       { 

        for each(var contact:Object in result.data) 
        { 
         var obj:Object = { 
          Name:contact["name"], 
          Value:contact["value"] 
         }; 
        allContacts.addItem(obj); 

        } 
       } 
      } 
    } 

    public function insert(name:String , value:String):void{ 
     var sqlText:String = "INSERT INTO contacts (name,value) VALUES ('" + 
      name + 
      "','" + 
      value + 
      "')"; 
     trace("Inserting into contacts by: "+sqlText); 
     var insertSQL:SQLStatement = new SQLStatement(); 
     insertSQL.sqlConnection = sqlConnection; 
     insertSQL.addEventListener(SQLEvent.RESULT, onInsert); 
     insertSQL.addEventListener(SQLErrorEvent.ERROR, onSQLError); 
     insertSQL.text = sqlText; 
     insertSQL.execute(); 
    } 

    private function onInsert(event:SQLEvent):void 
    { 
     trace("Record inserted"); 

    } 

} 
    } 

HomeView:

<?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="Contacts" 
    preinitialize="preinitializeHandler(event)"> 
<fx:Script> 
    <![CDATA[ 
     import ActionScript.dbHandler; 

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

     import spark.components.ViewNavigator; 
     import spark.events.IndexChangeEvent; 

     import views.NewContactView; 
     import views.PhoneBookHomeView; 


     private var conn:SQLConnection; 
     private var initComplete:Boolean = false; 
     private var sqlStat:SQLStatement; 
     private var sqlConnection:SQLConnection; 
     [Bindable] 
     public var contacts:ArrayList; 

     public var dbHandlerOBJ:dbHandler = new dbHandler(); 




     protected function button1_clickHandler(event:MouseEvent):void 
     { 
      navigator.pushView(NewContactView , null); 
     } 


     protected function preinitializeHandler(event:FlexEvent):void 
     { 
      PhoneBookList.dataProvider = dbHandlerOBJ.getAll(); 
     } 

    ]]> 
</fx:Script> 
<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 
<s:navigationContent> 
    <s:Button label="Search"/> 
</s:navigationContent> 
<s:List left="0" right="0" top="0" bottom="80" id="PhoneBookList" change=""> 
<s:itemRenderer> 
    <fx:Component> 
    <s:IconItemRenderer label="label" messageField="Message"/> 
    </fx:Component> 
</s:itemRenderer> 
</s:List> 
<s:Button left="5" right="5" top="677" bottom="10" label="Add New Contact" click="button1_clickHandler(event)"/> 

調試錯誤:

[SWF] PhoneBook.swf - 3,451,124 bytes after decompression 
dbHandler object was created 
DataBase exists 
Geting all contacts by: SELECT * FROM contacts 
TypeError: Error #1009: Cannot access a property or method of a null object reference. 
at views::PhoneBookHomeView/preinitializeHandler()[C:\Users\Adobe Flash      Builder 4.6\PhoneBook\src\views\PhoneBookHomeView.mxml:40] 
at views::PhoneBookHomeView/___PhoneBookHomeView_View1_preinitialize()[C:\Users\Adobe Flash Builder 4.6\PhoneBook\src\views\PhoneBookHomeView.mxml:4] 
at flash.events::EventDispatcher/dispatchEventFunction() 
at flash.events::EventDispatcher/dispatchEvent() 
at mx.core::UIComponent/dispatchEvent()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:13152] 
at mx.core::UIComponent/initialize()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7624] 
at spark.components::View/initialize()[E:\dev\4.y\frameworks\projects\mobilecomponents\src\spark\components\View.as:999] 
at views::PhoneBookHomeView/initialize() 
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::UIComponent/addChildAt()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:7199] 
at spark.components::Group/addDisplayObjectToDisplayList()[E:\dev\4.y\frameworks\projects\spark\src\spark\components\Group.as:2037] 
at spark.components::Group/http://www.adobe.com/2006/flex/mx/internal::elementAdded()[E:\dev\4.y\frameworks\projects\spark\src\spark\components\Group.as:1628] 
at spark.components::Group/addElementAt()[E:\dev\4.y\frameworks\projects\spark\src\spark\components\Group.as:1387] 
at spark.components::Group/addElement()[E:\dev\4.y\frameworks\projects\spark\src\spark\components\Group.as:1345] 
at spark.components::SkinnableContainer/addElement()[E:\dev\4.y\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:761] 
at spark.components::ViewNavigator/createViewInstance()[E:\dev\4.y\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:2018] 
at spark.components::ViewNavigator/commitNavigatorAction()[E:\dev\4.y\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:1932] 
at spark.components::ViewNavigator/commitProperties()[E:\dev\4.y\frameworks\projects\mobilecomponents\src\spark\components\ViewNavigator.as:1301] 
at mx.core::UIComponent/validateProperties()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:8219] 
at mx.managers::LayoutManager/validateProperties()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:597] 
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:783] 
at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180] 

我覺得分貝不存在.....但我不知道爲什麼我的類不工作:(

+0

你似乎沒有用getAll()函數返回任何東西,那麼評論部分是怎麼回事? //:ArrayCollection和//返回allContacts; –

+0

這並不是問題,即使他們沒有評論調試器給我所有的錯誤再次:( – user1631100

回答

3

你不能得到/讀取任何東西預初始化事件被觸發時的實例。

如果您參考官方文檔(http://livedocs.adobe.com/flex/3/html/help.html?content=containers_intro_3.html,部分「關於初始化和creationComplete事件」),或者鏈接:http://www.tutorialspoint.com/flex/flex_life_cycle_phases.htm,在觸發初始化過的事件的對象的實例是生(又名空)。

您應該將PhoneBookList.dataProvider = dbHandlerOBJ.getAll();插入到Initialized或更好的CreationComplete事件中。

+0

即使鏈接是關於Flex 3,這仍然是有效的最新版本。 –

+0

感謝您的幫助...它工作正常現在:) :) :) :) – user1631100

+1

然後很好,並確定了男人的答案。 :) – evilpenguin

相關問題