0

我正在做一个项目,我正在将数据从网络下载并保存到 sqlite 数据库。当我运行我的项目时,它显示 IllegalStateException(已经关闭)。

我的日志是:

    09-29 12:14:49.485: E/AndroidRuntime(4956): FATAL EXCEPTION: AsyncTask #2
09-29 12:14:49.485: E/AndroidRuntime(4956): java.lang.RuntimeException: An error occured while executing doInBackground()
09-29 12:14:49.485: E/AndroidRuntime(4956):     at android.os.AsyncTask$3.done(AsyncTask.java:266)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at java.lang.Thread.run(Thread.java:1020)
09-29 12:14:49.485: E/AndroidRuntime(4956): Caused by: java.lang.IllegalStateException: database /data/data/com.irlsolutions.ittrading/databases/ITTrading.sqlite (conn# 0) already closed
09-29 12:14:49.485: E/AndroidRuntime(4956):     at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2134)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1526)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1508)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at com.irlsolutions.ittradinglibrary.Query_new.getItemFromEndOfDay(Query_new.java:89)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at com.irlsolutions.ittrading.Symbol_Fetcher.getSymbolFromLocal(Symbol_Fetcher.java:70)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at com.irlsolutions.ittrading.Markets$Markets_Thread.doInBackground(Markets.java:190)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at com.irlsolutions.ittrading.Markets$Markets_Thread.doInBackground(Markets.java:1)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at android.os.AsyncTask$2.call(AsyncTask.java:252)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

doInBackGround() 是:

protected Void doInBackground(Void... arg0) {

    float flt_close1;
    float flt_close2;
    float variation;

    // TODO Auto-generated method stub

    xmlData = XML_Parser_Helper.parseXmlSax(Markets.this, "", R.raw.market_indices_america);        
    lst_codes = xmlData.getListOfItems();

    xmlData = XML_Parser_Helper.parseXmlSax(Markets.this, "", R.raw.market_indices_europe);
    lst_codes.addAll(xmlData.getListOfItems());

    xmlData = XML_Parser_Helper.parseXmlSax(Markets.this, "", R.raw.market_indices_asia);
    lst_codes.addAll(xmlData.getListOfItems());

    for(int i = 0; i < lst_codes.size(); i++){
    lst_symbols.add(
        symbolFetcher.getSymbolFromLocal(
            lst_codes.get(i).get("symbol_code").toString(), 
            lst_codes.get(i).get("market_code").toString(), 
            xmlData,
            "symbols",
            lst_codes.get(i).get("chain_code").toString()
        )
    );
    lst_symbols.get(i).put("company_name", lst_codes.get(i).get("company_name").toString());
    lst_symbols.get(i).put("symbol_code", lst_codes.get(i).get("symbol_code").toString());
    lst_symbols.get(i).put("market_code", lst_codes.get(i).get("market_code").toString());
    lst_symbols.get(i).put("chain_code", lst_codes.get(i).get("chain_code").toString());
    lst_symbols.get(i).put("flag", lst_codes.get(i).get("flag").toString());

    flt_close1 = (float)Double.parseDouble((lst_symbols.get(i).get("close_1").toString().equals(""))?"0.0":lst_symbols.get(i).get("close_1").toString());
    flt_close2 = (float)Double.parseDouble((lst_symbols.get(i).get("close_2").toString().equals(""))?"0.0":lst_symbols.get(i).get("close_2").toString());
    variation = Generic_Functions.getVariation(flt_close1, flt_close2);

    lst_symbols.get(i).put("variation", variation);

    if(symbolFetcher.isSymbolAddedToPortfolio(str_username, lst_codes.get(i).get("symbol_code").toString(), lst_codes.get(i).get("market_code").toString())){
        lst_symbols.get(i).put("added", true);
    }else{
        lst_symbols.get(i).put("added", false);
    }

    publishProgress(new Void[]{});
    }

    return null;
}

我正在使用查询的函数:

    public HashMap<String,Object> getItemFromEndOfDay(String str_symbolCode,String str_marketCode){
        HashMap<String,Object> hm = new HashMap<String, Object>();          

        Cursor cursor = database.rawQuery("SELECT * FROM " + Init.TABLE_END_OF_DAY + " WHERE symbol_code='" + str_symbolCode + "' AND market_code='" + str_marketCode + "'", null);


        if(cursor.getCount() > 0){
            cursor.moveToFirst();
            hm.put("symbol_code", cursor.getString(cursor.getColumnIndex("symbol_code")));
            hm.put("market_code", cursor.getString(cursor.getColumnIndex("market_code")));
            hm.put("instrument_name", cursor.getString(cursor.getColumnIndex("instrument_name")));
            hm.put("company_name", cursor.getString(cursor.getColumnIndex("company_name")));
            hm.put("isin", cursor.getString(cursor.getColumnIndex("isin")));
            hm.put("interval", cursor.getString(cursor.getColumnIndex("interval")));
            hm.put("exchange", cursor.getString(cursor.getColumnIndex("exchange")));
            hm.put("ex_open", cursor.getString(cursor.getColumnIndex("ex_open")));
            hm.put("ex_close", cursor.getString(cursor.getColumnIndex("ex_close")));
            hm.put("currency", cursor.getString(cursor.getColumnIndex("currency")));
            hm.put("timezone", cursor.getString(cursor.getColumnIndex("timezone")));

            hm.put("date_1", cursor.getString(cursor.getColumnIndex("date_1")));
            hm.put("hour_1", cursor.getString(cursor.getColumnIndex("hour_1")));
            hm.put("open_interest_1", cursor.getString(cursor.getColumnIndex("open_interest_1")));
            hm.put("open_1", cursor.getString(cursor.getColumnIndex("open_1")));
            hm.put("high_1", cursor.getString(cursor.getColumnIndex("high_1")));
            hm.put("low_1", cursor.getString(cursor.getColumnIndex("low_1")));
            hm.put("close_1", cursor.getString(cursor.getColumnIndex("close_1")));
            hm.put("volume_1", cursor.getString(cursor.getColumnIndex("volume_1")));

            hm.put("date_2", cursor.getString(cursor.getColumnIndex("date_2")));
            hm.put("hour_2", cursor.getString(cursor.getColumnIndex("hour_2")));
            hm.put("open_interest_2", cursor.getString(cursor.getColumnIndex("open_interest_2")));
            hm.put("open_2", cursor.getString(cursor.getColumnIndex("open_2")));
            hm.put("high_2", cursor.getString(cursor.getColumnIndex("high_2")));
            hm.put("low_2", cursor.getString(cursor.getColumnIndex("low_2")));
            hm.put("close_2", cursor.getString(cursor.getColumnIndex("close_2")));
            hm.put("volume_2", cursor.getString(cursor.getColumnIndex("volume_2")));

            cursor.close();
            return hm;
        }else{
                hm.put("symbol_code", str_symbolCode);
            hm.put("market_code", str_marketCode);
            hm.put("instrument_name","");
            hm.put("company_name", "");
            hm.put("isin", "");
            hm.put("interval", "");
            hm.put("exchange", "");
            hm.put("ex_open", "");
            hm.put("ex_close", "");
            hm.put("currency", "");
            hm.put("timezone", "America/New_York");

            hm.put("date_1", "");
            hm.put("hour_1", "");
            hm.put("open_interest_1", "");
            hm.put("open_1", "");
            hm.put("high_1", "");
            hm.put("low_1", "");
            hm.put("close_1", "");
            hm.put("volume_1", "");

            hm.put("date_2", "-");
            hm.put("hour_2", "");
            hm.put("open_interest_2", "");
            hm.put("open_2", "");
            hm.put("high_2", "");
            hm.put("low_2", "");
            hm.put("close_2", "");
            hm.put("volume_2", "");

            cursor.close();
            return hm;
        }
//      cursor.close();
//      return hm;
    }
4

2 回答 2

1

异常表明您在游标已关闭时调用 close 。异常中的行号会告诉你在哪里;我猜我会说它在 else 块中

于 2012-09-29T07:11:08.210 回答
1

问题不是关闭它两次,问题是关闭后使用它。

如果您查看查询:

09-29 12:14:49.485: E/AndroidRuntime(4956): Caused by: java.lang.IllegalStateException: database /data/data/com.irlsolutions.ittrading/databases/ITTrading.sqlite (conn# 0) already closed
09-29 12:14:49.485: E/AndroidRuntime(4956):     at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2134)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1526)
09-29 12:14:49.485: E/AndroidRuntime(4956):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1508)

你看它来自rawquery,一个你在开始时使用的函数getItemFromEndOfDay。所以如果我没记错的话,你会在调用函数的开始出现这个错误,因为那时数据库还没有打开。您可以通过检查您获得的代码的距离来轻松验证这一点。

确保您没有关闭任何东西,然后再次尝试使用该数据库。添加一些调试行以验证它确实是这一行,以及是否在此之前关闭了任何内容。

于 2012-09-29T07:25:33.100 回答