关闭()从未在数据库上调用

时间:2022-08-23 11:45:12

I have tried closing my cursors after I retrieve my selected value on my spinner to see if that would help but it didn't. Since my spinners rely on other spinners to get the right values it crashes my app because there is no value after closing the cursor.

我在我的微调器上检索我选择的值之后尝试关闭我的游标,看看是否会有所帮助,但事实并非如此。由于我的旋转器依赖其他旋转器来获得正确的值,因此在关闭光标后没有任何值会使我的应用程序崩溃。

This only happens on when the API is <16. Also it only happens right after I click on a spinner in my layout even before I select an item in the spinner dialog.

这仅在API <16时发生。它也只是在我在我的布局中点击一个微调器之后,甚至在我在微调器对话框中选择一个项目之前。

onDestroy()

protected void onDestroy() 
{
    super.onDestroy();
    myDataBase.close();
}

close()

@Override
public void close() {

if(myDataBase != null){
    myDataBase.close();
}
}

Here is the LogCat:

这是LogCat:

06-13 15:18:36.256: E/SQLiteDatabase(736): close() was never explicitly called on database '/data/data/com.example.productguide/databases/products.db' 
06-13 15:18:36.256: E/SQLiteDatabase(736): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1980)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:977)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:745)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at com.example.productguide.DataBaseHelper.createDataBase(DataBaseHelper.java:146)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at com.example.productguide.ProductActivity.onCreate(ProductActivity.java:83)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.app.Activity.performCreate(Activity.java:4397)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.os.Looper.loop(Looper.java:132)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at android.app.ActivityThread.main(ActivityThread.java:4123)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at java.lang.reflect.Method.invokeNative(Native Method)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at java.lang.reflect.Method.invoke(Method.java:491)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-13 15:18:36.256: E/SQLiteDatabase(736):  at dalvik.system.NativeStart.main(Native Method)

EDIT: This also only seems to happen once even after closing and re-opening the app.

编辑:即使关闭并重新打开应用程序后,这似乎也只发生过一次。

1 个解决方案

#1


0  

I used this tutorial to use my existing database to work. After using a few tips from user comments on the tutorial I started to receive the Close() was never called on database error.

我使用本教程来使用我现有的数据库来工作。在使用了教程中用户注释的一些提示后,我开始收到Close()从未调用过数据库错误。

After paying attention to my LogCat, I have found my fix. I wasn't closing my database in my createDatabase() and in my copyDatabase(). I am not sure if the fixes I added to my code will cause other problems for me in the future but testing it on the supported API's for my app, I have not received one error.

在关注我的LogCat之后,我找到了我的修复程序。我没有在createDatabase()和copyDatabase()中关闭我的数据库。我不确定我添加到我的代码中的修复程序是否会在将来导致其他问题但是在我的应用程序支持的API上测试它,我没有收到一个错误。

This is what I added to my code to make it work without any errors:

这是我添加到我的代码中使其工作没有任何错误:

Instead of this.getWritableDatabase(); in createDatabase() I used this.getWritableDatabase().close();

而不是this.getWritableDatabase();在createDatabase()中我使用了this.getWritableDatabase()。close();

And I added

我补充道

if(myDatabase != null)
{
    myDatabase.close();
}

in my try/catch under checkdb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); in copyDatabase()

在我的try / catch下checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);在copyDatabase()中

#1


0  

I used this tutorial to use my existing database to work. After using a few tips from user comments on the tutorial I started to receive the Close() was never called on database error.

我使用本教程来使用我现有的数据库来工作。在使用了教程中用户注释的一些提示后,我开始收到Close()从未调用过数据库错误。

After paying attention to my LogCat, I have found my fix. I wasn't closing my database in my createDatabase() and in my copyDatabase(). I am not sure if the fixes I added to my code will cause other problems for me in the future but testing it on the supported API's for my app, I have not received one error.

在关注我的LogCat之后,我找到了我的修复程序。我没有在createDatabase()和copyDatabase()中关闭我的数据库。我不确定我添加到我的代码中的修复程序是否会在将来导致其他问题但是在我的应用程序支持的API上测试它,我没有收到一个错误。

This is what I added to my code to make it work without any errors:

这是我添加到我的代码中使其工作没有任何错误:

Instead of this.getWritableDatabase(); in createDatabase() I used this.getWritableDatabase().close();

而不是this.getWritableDatabase();在createDatabase()中我使用了this.getWritableDatabase()。close();

And I added

我补充道

if(myDatabase != null)
{
    myDatabase.close();
}

in my try/catch under checkdb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); in copyDatabase()

在我的try / catch下checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);在copyDatabase()中