31

I sometimes see this error in my logcat output,

Cursor: invalid statement in fillWindow().

It sometimes happens when I press the back key and then it goes to the default Android listview before going to my custom listview.

What does it mean? How do I solve it? Because it does not point to any line of code where the problem is coming from.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
irobotxx
  • 5,963
  • 11
  • 62
  • 91
  • 1
    Having the same problem, i just found this. It relates to how your db is probably running in the wrong scope. http://groups.google.com/group/android-developers/browse_thread/thread/8bec793c626fb405 – Emile Nov 18 '10 at 13:14

5 Answers5

32

When dealing with ListActivities, this issue has to do with the Cursor objects, CursorAdapter objects, and Database objects not being closed properly when the Activity stops, and not being set properly when the Activity starts or resumes.

I had to make sure that I closed my SimpleListAdapter, my Cursors, and then my Database objects in that respective order, in the onStop method of the Activity that is called when the TabActivity resumes.

I had already been closing the Cursor and Database objects, but had not been closing my SimpleListAdapter Cursor.

/**
   * onStop method
   * 
   * Perform actions when the Activity is hidden from view
   * 
   * @return void
   * 
   */
  @Override
  protected void onStop() {
    try {
      super.onStop();

      if (this.mySimpleListAdapterObj !=null){
        this.mySimpleListAdapterObj.getCursor().close();
        this.mySimpleListAdapterObj= null;
      }

      if (this.mActivityListCursorObj != null) {
        this.mActivityListCursorObj.close();
      }

      if (this.myDatabaseClassObj != null) {
        this.myDatabaseClassObj.close();
      }
    } catch (Exception error) {
      /** Error Handler Code **/
    }// end try/catch (Exception error)
  }// end onStop
Bryan
  • 3,629
  • 2
  • 28
  • 27
  • 2
    I just wanted to thank you, I just spent way too long getting frustrated at this same problem and yours is the only advice I read that said to close things in a certain order, which worked. Thanks! – Mark D Oct 27 '11 at 21:34
  • 2
    Hi there. As quite a newbee to Android, I just learned in a Workshop that "onStop" is not guaranteed to be called. They said, it was better to do that stuff in "onPause". Just saying - if I am wrong please correct me. I'm always willing to learn from experienced people. – Fildor Nov 22 '11 at 08:43
  • 1
    If this issue (which seems quite important!) were well-documented, this question wouldn't have come up. Thank you! I only wish I had found this *before* I wasted untold hours writing crappy code. – SMBiggs May 21 '12 at 16:56
  • i slight doubt what is` dbActListObj` refers to here? – Akhil Jain Feb 05 '13 at 05:25
  • Akhil Jain, sorry about the confusing code. I edited it and used a better, easier to understand variable name. – Bryan Feb 05 '13 at 22:28
19

It is of utmost importance that you close the Cursors, Databases, DBHelpers in the right order.

for e.g. for the given code below.

DBHelper dbhelper = new DBHelper();
SQLiteDataBase db = dbhelper.getWritableDatabase();

Cursor c = db.query(/*some parameters*/);

the order of closing should be like:

c.close();
db.close();
dbhelper.close();

Otherwise different errors keep on spawning and the developer does not even come to know about it. "Cursor: invalid statement in fillWindow()" being one of such errors.

JaydeepW
  • 3,237
  • 1
  • 25
  • 27
  • 2
    what if i am returning cursor from method for the custom cursor adapter(extends cursoradapter) for my list activity, how to deal with that – Akhil Jain Feb 05 '13 at 06:09
7

Maybe this can help you: http://www.ragtag.info/2011/feb/1/database-pitfalls/

It seems that calls to getReadableDatabase and getWritableDatabase returns the same connection to the database (even if you made several calls to them). So, any call to close() on any of them will close both connection(s).

If you tries to use a cursor later, you'll get the nice 'Invalid statement', since the connection which the cursor relies on, is already closed.

Jesus Monzon Legido
  • 1,253
  • 12
  • 10
1

I am still having issues with the 'Invalid statement in fillWindow()' error.

I have narrowed the issue down to the SimpleCursorAdapter cursor for my ListView.

For example, if I am in the listview for an Activity A, and I close the cursor before starting a new Activity B, I don't get the 'Invalid statement in fillWindow()' when I return to Activity A.

However, before Activity B loads, I see the list from Activity A's listview disappear on the screen, and the 'No Records Found' message is displayed briefly before the screen is hidden, before Activity B's screen is shown.

How can I gracefully resolve this issue?

EDIT: I actually figured this out this morning. I added

this.stopManagingCursor(this.myListCursor);

to the onPause method in my ListActivity classes, and that resolved the 'Invalid statement in fillWindow()' error.

Bryan
  • 3,629
  • 2
  • 28
  • 27
  • I actually figured this out this morning. I added 'this.stopManagingCursor(this.myListCursor)' to the onPause method in my ListActivity classes, and that resolved the 'Invalid statement in fillWindow()' error. – Bryan Dec 14 '12 at 16:52
1

If you are using a custom Class instance e.g. Model m that holds a DatabaseManager, which in turn holds a SQLiteDatabase: Model->DatabaseManager->SQLiteDatabase

Then, if you do a query to m (which does the appropiate delegations) and then you do something like m.close() (which actually closes the SQLiteDatabase) and after that you try to use the Cursor you will get that error.

The solution is: first use the cursor and then close the Db.

My response is based in the 2 existing so far, that inspired me to solve the problem.

Caumons
  • 9,341
  • 14
  • 68
  • 82