Java 如何解决此异常?
我一直收到此异常,DatabaseObjectNotClosedException:Java 如何解决此异常?,java,android,database,sqlite,exception,Java,Android,Database,Sqlite,Exception,我一直收到此异常,DatabaseObjectNotClosedException: close() was never explicitly called on database '/data/data/com.project.test/databases/database' E/SQLiteDatabase(13921): android.database.sqlite.DatabaseObjectNotClosedException: Application did no
close() was never explicitly called on database '/data/data/com.project.test/databases/database'
E/SQLiteDatabase(13921): android.database.sqlite.DatabaseObjectNotClosedException:
Application did not close the cursor or database object that was opened here
我试图关闭数据库帮助器和游标,但会出现运行时异常。当我离开活动并在按下后退按钮后重新访问它时,就会发生这种情况
如何正确关闭游标和助手
我尝试了两种方法:
首先,在每次单独使用后关闭游标,并在暂停时关闭数据库帮助程序
第二,在暂停时与databasehelper一起关闭游标,但两者都不起作用
有人能帮我吗
编辑:
我在onCreate()函数中打开了一个数据库帮助程序。 填充listview时使用的游标, 用于查找现有记录的游标, 获取信息的游标
更新:
我已尝试关闭Pause和onDestroy,但它仍然会因runtimeexception而崩溃。是否关闭SQLiteDatabase对象。如果没有,请尝试像这样关闭SQLiteDatabase对象
您还说过,当您关闭数据库或光标时,会出现运行时异常。这是相同的例外还是不同的例外?您可以发布您的code@Gaurav您实际需要查看代码的哪一部分?因为它有很多代码。我可以看到我可以粘贴的内容。@Gaurav我已经写下了我使用databasehelper和cursors@Gaurav我也从我的活动中添加了一些代码。谢谢你的回复,我已经在问题中添加了一些信息。我尝试在onPause()方法上关闭光标。当我去参加另一项活动并按下后退按钮时。它向我抛出一个运行时异常。如果我不关闭,那么除了DatabaseObjectNotClosedException之外,它不会有任何问题。请尝试将datbase帮助程序转移到扩展SQLiteOpenHelper的新活动,然后在主活动类中实例化SQLiteHelper。这是我的想法,但我不确定它是否有效。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this.getActivity();
context = this.getActivity().getApplicationContext();
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDbHelper = new DatabaseHelper(context);
mDbHelper.open();
populateList();
}
public void populateList() {
directoryCursor = mDbHelper.fetchAllRootDirectories();
activity.startManagingCursor(directoryCursor);
adapter = new DirectoryListAdapter(this.getActivity(), directoryCursor);
this.setListAdapter(adapter);
}
......
private UpdateDatabaseListener updateDatabaseListener = new UpdateDatabaseListener() {
public void onUpdate(int from, int to) {
.....
findExistingRecordCursor = mDbHelper.findExistingRecords(from, to);
activity.startManagingCursor(findExistingRecordCursor);
if(findExistingRecordCursor.getCount() == 0) {
....
}
}
}