Android:我们可以在运行UI线程时将数据插入到sqlite中吗?

时间:2012-10-28 09:19:37

标签: android sqlite xml-parsing ui-thread

我发现了同样的问题,但没有答案。这就是为什么我想在这里再问:

这是我主线程上的课程:

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 global;

                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    }

任何人都可以帮助我吗? 提前谢谢......

1 个答案:

答案 0 :(得分:2)

我也遇到了数据库被锁定的问题。我使用内容提供商来摆脱它。可能这对你也有帮助。

我提到了Google IO应用http://code.google.com/p/iosched/