2012-10-28 59 views
1

我發現了同樣的問題,但沒有回答。這就是爲什麼我想再次詢問這裏:Android:我們可以在運行UI線程時將數據插入sqlite嗎?

這裏是我的主線程類:

private class DownloadTask extends AsyncTask<String, Void, String> 
    { 
     @Override 
     protected String doInBackground(String... urls) 
     { 
      // TODO Auto-generated method stub 
      XMLParser parser1 = new XMLParser();//System.out.println("XML before catching"); 
      String result = parser1.getXmlFromUrl("http://exampl1.com/arrival.XML"); 
      xml=result; 
      Document doc = parser1.getDomElement(xml); 
      NodeList nl = doc.getElementsByTagName(DOM_KEY_CONTENT); 
      arrayList = new ArrayList<HashMap<String,String>>(); 
      databaseAirport myDbHelper ; 
      myDbHelper = new databaseAirport(ctx); 
      myDbHelper.open(); 
      for (int i = 0; i < nl.getLength(); i++) 
      { 
       // creating new HashMap 
       HashMap<String, String> mapData= new HashMap<String, String>(); 
       Element e = (Element) nl.item(i); 
       // adding each child node to HashMap key => value 
       mapData.put(DOM_KEY_ID, parser1.getValue(e, DOM_KEY_ID)); 
       mapData.put(DOM_KEY_PRI_ID,parser1.getValue(e,DOM_KEY_PRI_ID)); 
       mapData.put(DOM_KEY_FDATE, parser1.getValue(e, DOM_KEY_FDATE)); 
       mapData.put(DOM_KEY_ORDER, parser1.getValue(e, DOM_KEY_ORDER)); 

       arrayList.add(mapData); 

字符串A1,B1,C1,D1是VAR全球;

   a1 = parser1.getValue(e, DOM_KEY_ID); 
       b1 = parser1.getValue(e, DOM_KEY_PRI_ID); 
       c1 = parser1.getValue(e, DOM_KEY_FDATE); 
       d1 = parser1.getValue(e, DOM_KEY_ORDER); 

       System.out.println(a1+b1+c1+d1); 
       myDbHelper.insertArrival(Integer.parseInt(a1), c1, Integer.parseInt(d1)); 

      } 
      long size = arrayList.size(); 
      System.out.println("Size of arrayList "+size); 
      return result; 
     } 
    } 

A1,B1,C1和D1打印爲正常

,這裏是錯誤:

java.lang.RuntimeException: An error occured while executing doInBackground() 
at android.os.AsyncTask$3.done(AsyncTask.java:299) 
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
at java.lang.Thread.run(Thread.java:856) 

**Caused by: java.lang.NullPointerException** 


at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 
    at com.airport.serverside.databaseAirport.open(databaseAirport.java:76) 
    at com.airport.serverside.AirportActivity$DownloadWebPageTask.doInBackground(AirportActivity.java:133) 
    at com.airport.serverside.AirportActivity$DownloadWebPageTask.doInBackground(AirportActivity.java:1) 
    at android.os.AsyncTask$2.call(AsyncTask.java:287) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 

編輯

74 public databaseAirport open() throws SQLException 
75 { 
76  db = DBHelper.getWritableDatabase(); 
77  return this; 
78 } 

誰能幫我? 在此先感謝...

回答

2

我還面臨着這樣一個數據庫被鎖定的問題。我使用內容提供商來擺脫這一點。也許這對你也有幫助。

我參考了Google IO應用程序http://code.google.com/p/iosched/

+0

我堅持了這麼久的日子。你是否認爲,這是因爲主線程不允許訪問數據庫? –

+0

不,這是因爲數據庫被鎖定用於寫入並且從多個線程並行訪問。我可以向你保證,使用內容提供者可以解決這個問題。 –

+0

hmmmm ....如果沒有其他方法可以通過訪問數據庫來解決,我會嘗試使用它 –

相關問題