开发者

How to solve this exception?

开发者 https://www.devze.com 2023-04-01 03:38 出处:网络
I have been getting this exception, DatabaseObjectNotClosedException: close() was never explicitly called on database \'/data/data/com.project.test/databases/database\'

I have been getting this 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 not close the cursor or database object that was opened here

I tried closing the database helper and cursors, but I will get runtime exception. This happ开发者_如何学Pythonens when I leave the activity and revisit it after hitting the back button.

How can I close my cursors and helpers properly?

I have tried two methods:

first, closing the cursors after each individual use, and closing the database helper onpause.

second, closing the cursors onpause along with the databasehelper, but both didn't work.

can someone help me with this?

EDIT:

@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) {

              ....

          }

  }
}

I have a database helper opened in the onCreate() function. cursors used when populating the listview, cursors used to find existing records, cursors to get information.


UPDATE:

I have tried closing onPause and onDestroy, it still crashes with runtimeexception.


Are you closing the SQLiteDatabase object or not. If not try to close SQLiteDatabase Object like this

  1. SQLiteDatabase db = SQLiteHelper classobject.getWriteableDatabase();

  2. // block of code

  3. db.close();

and run your application.

Could you please post your code so that it will be more helpful to understand your problem.


Sorry for the late reply. From what I see, you have opened the db using mDbHelpher.open() After that you did populatelist() did you try doing mDbHelpher.close() after that?

Same thing with the cursors. Because your error clearly says that the db or the cursor was left open. As soon as you are done using the db, you should close. This should not give the problem even after you visit another activity and then press back button.

Also you said you get runtime exception when you close the db or the cursor. Is it the same exception or different?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号