“fillWindow() 中的无效语句”是什么意思? Android中的光标是什么意思?

发布于 10-02 22:30 字数 249 浏览 4 评论 0原文

我有时会在 logcat 输出中看到此错误,

Cursor: invalid statement in fillWindow().

有时当我按下后退键,然后它会在转到我的自定义 之前转到默认的 Android listview 时,会发生此错误列表视图。

这是什么意思?我该如何解决?因为它没有指向问题所在的任何代码行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风蛊2024-10-09 22:30:02

处理 ListActivities 时,此问题与活动停止时未正确关闭 Cursor 对象、CursorAdapter 对象和数据库对象以及活动启动或恢复时未正确设置有关。

我必须确保在 TabActivity 恢复时调用的 Activity 的 onStop 方法中按各自的顺序关闭 SimpleListAdapter、Cursors 和 Database 对象。

我已经关闭了 Cursor 和 Database 对象,但还没有关闭我的 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

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
摇划花蜜的午后2024-10-09 22:30:02

以正确的顺序关闭游标、数据库、DBHelpers 是至关重要的。

例如
对于下面给定的代码。

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

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

关闭的顺序应该是这样的:

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

否则,不同的错误会不断产生,而开发人员甚至不会知道它。 “光标:fillWindow() 中的语句无效”就是此类错误之一。

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.

吹泡泡o2024-10-09 22:30:02

也许这可以帮助您: http://www.ragtag.info/2011/feb/1/database -pitfalls/

似乎对 getReadableDatabasegetWritableDatabase 的调用返回与数据库的相同连接(即使您进行了多次调用给他们)。
因此,对其中任何一个的 close() 的任何调用都会关闭这两个连接。

如果稍后尝试使用游标,您将得到漂亮的“无效语句”,因为游标所依赖的连接已经关闭。

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.

聊慰2024-10-09 22:30:02

如果您使用自定义类实例,例如包含 DatabaseManagerModel m,而该实例又包含 SQLiteDatabase:Model->DatabaseManager- >SQLiteDatabase

然后,如果您对 m 进行查询(执行适当的委托),然后执行类似 m.close() 的操作(这实际上会关闭 SQLiteDatabase ),之后您尝试使用光标,您将收到该错误。

解决办法是:先使用游标,然后关闭Db。

我的回答是基于迄今为止的 2 个现有回答,这激发了我解决问题的灵感。

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.

忆梦2024-10-09 22:30:02

我仍然遇到“fillWindow() 中的无效语句”错误的问题。

我已将问题范围缩小到 ListView 的 SimpleCursorAdapter 光标。

例如,如果我位于活动 A 的列表视图中,并且在启动新的活动 B 之前关闭游标,则当我返回活动 A 时,我不会收到“FillWindow() 中的无效语句”。

但是,在之前活动 B 加载,我看到活动 A 的列表视图中的列表在屏幕上消失,并且在屏幕隐藏之前、活动 B 的屏幕显示之前短暂显示“未找到记录”消息。

我怎样才能优雅地解决这个问题?

编辑:
我今天早上实际上就明白了这一点。我将

this.stopManagingCursor(this.myListCursor);

添加到 ListActivity 类中的 onPause 方法,并解决了“fillWindow() 中的无效语句”错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文