Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何解决close()从未在数据库错误时显式调用_Android_Sqlite - Fatal编程技术网

Android 如何解决close()从未在数据库错误时显式调用

Android 如何解决close()从未在数据库错误时显式调用,android,sqlite,Android,Sqlite,我在这里和其他博客上找到了很多答案。他们建议我在所有打开的连接上调用close()函数。我这样做了,但仍然得到这个错误。我有一个应用程序,它首先尝试计算sqlite数据库表中存在的提示数,如果没有行,则返回0。如果有行,则返回行数。这是我的密码 public int count_tips() { Cursor cursor = ourDatabase.rawQuery("select * from " + DATABASE_TIPS_TABLE, null);

我在这里和其他博客上找到了很多答案。他们建议我在所有打开的连接上调用close()函数。我这样做了,但仍然得到这个错误。我有一个应用程序,它首先尝试计算sqlite数据库表中存在的提示数,如果没有行,则返回0。如果有行,则返回行数。这是我的密码

public int count_tips() {

    Cursor cursor = ourDatabase.rawQuery("select * from "
            + DATABASE_TIPS_TABLE, null);

    if (cursor != null) {
        cursor.moveToFirst();
        if (cursor.getInt(0) == 0) {

            cursor.close();
            return 0;
        } else {

            cursor.close();
            return cursor.getCount();
        }
    } else {
        cursor.close();
    }

    return 0;

}
但我不断地得到下面的错误,哪里可能出错。至少我想我已经关闭了我打开的所有数据库连接。但我不知道我哪里出了问题

03-02 11:09:00.457: D/Cursor(4712): Table name   : null
03-02 11:09:00.457: D/Cursor(4712): SQL          : SQLiteQuery: select * from tips
03-02 11:09:00.457: I/dalvikvm(4712): Uncaught exception thrown by finalizer (will be discarded):
03-02 11:09:00.457: I/dalvikvm(4712): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@4054c690 on null that has not been deactivated or closed
03-02 11:09:00.464: I/dalvikvm(4712):   at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:620)
03-02 11:09:00.464: I/dalvikvm(4712):   at dalvik.system.NativeStart.run(Native Method)
03-02 11:09:00.472: E/Database(4712): close() was never explicitly called on database '/data/data/com.jingo.willappsug.yamba/databases/yambasqlite' 
03-02 11:09:00.472: E/Database(4712): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-02 11:09:00.472: E/Database(4712):   at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
03-02 11:09:00.457:D/游标(4712):表名:null
03-02 11:09:00.457:D/光标(4712):SQL:SQLiteQuery:select*fromtips
03-02 11:09:00.457:I/dalvikvm(4712):终结器引发的未捕获异常(将被丢弃):
03-02 11:09:00.457:I/dalvikvm(4712):Ljava/lang/IllegalStateException;:正在终结游标android.database.sqlite。SQLiteCursor@4054c690未被停用或关闭的空
03-02 11:09:00.464:I/dalvikvm(4712):在android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:620)
03-02 11:09:00.464:I/dalvikvm(4712):在dalvik.system.NativeStart.run(本机方法)
03-02 11:09:00.472:E/Database(4712):从未对数据库“/data/data/com.jingo.willappsug.yamba/databases/yambasqlite”显式调用close()
03-02 11:09:00.472:E/Database(4712):android.Database.sqlite.DatabaseObjectNotClosedException:应用程序未关闭在此打开的光标或数据库对象
03-02 11:09:00.472:E/Database(4712):位于android.Database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1847)

是否尝试将
close()
调用放入带有
try/catch
finally

public int count_tips() {
   try {
       ... // your code here
   }
   catch (Exception e) {
       // handle the exception properly
   }
   finally {
       cursor.close()
   }
}

在尝试修复此错误三天后,工作正常。谢谢。此外,即使您不打算在这里处理异常(假设您的方法声明它抛出异常),在没有catch blocknew问题的情况下放置try和finally块仍然是一种很好的做法,我不能再将数据插入数据库_TIPS_表jsonarray tipsJSon_array=json.getJSONArray(“TIPS”);对于(int i=0;i