0

アプリでsqliteデータベースにアクセスしようとしていますが、次の例外が発生します。

12-27 11:32:12.760: E/Exception:(746): java.lang.IllegalStateException: attempt to acquire a reference on a close SQLiteClosable Exception occured in ContactListOfNumbersForWhichRuleIsAlreadySpecified() of DatabaseHandlerRule.java

私はこのコードを使用しています:

public ArrayList<String> ContactListOfNumbersForWhichRuleIsAlreadySpecified(DatabaseHandlerRule Activity) 
    {
        ContactRule contact = null;
        Cursor cursor =  null;
        SQLiteDatabase db =  null;
        ArrayList<String> contactList =  null;
        try
        {
            contactList = new ArrayList<String>();

            //      SQLiteActivity1.ReadingAllContactsRule(SplashActivity.s_dbRule);

            String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS + " WHERE "+KEY_NAME+" !='"
            +"abcde#$%&*()@#$%"+"'"+" AND "+KEY_PH_NO+"!='"+"abcde#$%&*()@#$%"+"'"+" AND "+KEY_DATE+" ='"+"0"+"'";

            db = this.getReadableDatabase();

            if (!db.isOpen()) {
                db = SQLiteDatabase.openDatabase(
                        "/data/data/com.velosys.smsManager/databases/rulesManager",
                        null, SQLiteDatabase.OPEN_READWRITE);
            }

            cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    contact = new ContactRule();
                    contact.setID(Integer.parseInt(cursor.getString(0)));
                    contact.setName(cursor.getString(1));
                    contact.setPhoneNumber(cursor.getString(2));
                    contact.setFolderName(cursor.getString(3));
                    contact.setParentFolderAddress(cursor.getString(4));
                    contact.setTime(cursor.getLong(5));
                    contact.setDate(cursor.getLong(6));
                    // Adding contact to list
                    contactList.add(contact.getName());
                } while (cursor.moveToNext());
            }
            else if(!cursor.moveToFirst())
            {
                Log.e("Message: ","Rule is not specified for even a single number in database");
                return contactList;             
            }
        }
        catch(Exception e)
        {
            Log.e("Exception: ",e+" Exception occured in ContactListOfNumbersForWhichRuleIsAlreadySpecified() of DatabaseHandlerRule.java");
        }
        finally
        {   
            if(cursor != null && !cursor.isClosed())
                cursor.close(); 
            if(db != null && db.isOpen())
                db.close();
        }
        return contactList;
    }

私はこの例外の背後にある理由を検索しましたが、私の場合には何も意味しません。

  1. 私はデータベースを書き込もうとはしていませんが、読み取ろうとしているので、並行性についてここで疑問は生じません。
  2. でデータベースヘルパークラスのインスタンスを使用していますdb = this.getReadableDatabase();
  3. データベースを開くたびに、データベースを適切に閉じています。

助けてください。よろしくお願いします。

編集:

最後に削除し、最後にブロックせずにコードを配置した後、すべてが正常に機能しています。この背後にある理由を教えてもらえますか?

          //finally
            {   
                if(cursor != null && !cursor.isClosed())
                    cursor.close(); 
                if(db != null && db.isOpen())
                    db.close();
            }
4

3 に答える 3

3

最後にブロックを削除してください。dbとカーソルを閉じると、このエラーが発生します。

于 2012-12-27T06:17:43.037 に答える
0

ステートメントdb = this.getReadableDatabase();では、使用しているコンテキストがわかりません。したがって、次のようなグローバルコンテキストを宣言し、Context context = this;そのステートメントを次のように呼び出します。db = context.getReadableDatabase();

于 2012-12-27T06:19:10.713 に答える
0

最後に削除し、最後にブロックせずにコードを配置した後、すべてが正常に機能しています。この背後にある理由を教えてもらえますか?

  //finally
            {   
                if(cursor != null && !cursor.isClosed())
                    cursor.close(); 
                if(db != null && db.isOpen())
                    db.close();
            }
于 2012-12-27T06:41:25.563 に答える