关闭游标上的 IllegalStateException

发布于 2024-09-06 04:32:47 字数 1745 浏览 7 评论 0原文

我的问题似乎与以下问题相关:

如何在代码中处理游标上的 IllegalStateException?

违规方法的代码如下:

    public boolean isPermanent(String screen_name) {
    boolean output = false;
    try {
        Cursor c = mDb.query(USERS, new String[] {PERMANENT}, NAME + "='" + screen_name + "'", null, null, null, null);
        if (c != null && c.getCount() > 0) {
            c.moveToFirst();
            output = c.getString(0).contentEquals("C");
            c.close();
        }
    }
    catch (Exception e) {
        Log.e("DBERR", e.getMessage());
    }
    return output;
}

但是,当抛出异常时,我正在查看有问题的游标,我可以在 Eclipse 中看到以下内容:

this    SQLiteCursor  (id=830061188288) 
mClosed true    
mColumnNameMap  null    
mColumns    String[1]  (id=830061188608)    
mContentObservable  ContentObservable  (id=830061188456)    
mContentResolver    null    
mCount  0   
mCurrentRowID   null    
mCursorState    0   
mDatabase   SQLiteDatabase  (id=830060407768)   
mDataSetObservable  DataSetObservable  (id=830061188408)    
mDriver SQLiteDirectCursorDriver  (id=830061143904) 
mEditTable  "users" (id=830060403008)   
mInitialRead    2147483647  
mLock   null    
mMaxRead    2147483647  
mNotificationHandler    null    
mNotifyUri  null    
mPendingData    false   
mPos    -1  
mQuery  SQLiteQuery  (id=830061143936)  
mRowIdColumnIndex   -1  
mSelfObserver   null    
mSelfObserverLock   Object  (id=830061188504)   
mSelfObserverRegistered false   
mStackTraceElements null    
mUpdatedRows    HashMap  (id=830061144056)  
mWindow null    

这清楚地表明了游标已关闭,因为它应该是关闭的 - 那么有人知道为什么这应该抛出异常吗?

My question appears to be linked to the following question:

How to Handle in code IllegalStateException on Cursor?

The code for the offending method is as follows:

    public boolean isPermanent(String screen_name) {
    boolean output = false;
    try {
        Cursor c = mDb.query(USERS, new String[] {PERMANENT}, NAME + "='" + screen_name + "'", null, null, null, null);
        if (c != null && c.getCount() > 0) {
            c.moveToFirst();
            output = c.getString(0).contentEquals("C");
            c.close();
        }
    }
    catch (Exception e) {
        Log.e("DBERR", e.getMessage());
    }
    return output;
}

However, I am looking at the Cursor in question when the exception is thrown, I can see the following in Eclipse:

this    SQLiteCursor  (id=830061188288) 
mClosed true    
mColumnNameMap  null    
mColumns    String[1]  (id=830061188608)    
mContentObservable  ContentObservable  (id=830061188456)    
mContentResolver    null    
mCount  0   
mCurrentRowID   null    
mCursorState    0   
mDatabase   SQLiteDatabase  (id=830060407768)   
mDataSetObservable  DataSetObservable  (id=830061188408)    
mDriver SQLiteDirectCursorDriver  (id=830061143904) 
mEditTable  "users" (id=830060403008)   
mInitialRead    2147483647  
mLock   null    
mMaxRead    2147483647  
mNotificationHandler    null    
mNotifyUri  null    
mPendingData    false   
mPos    -1  
mQuery  SQLiteQuery  (id=830061143936)  
mRowIdColumnIndex   -1  
mSelfObserver   null    
mSelfObserverLock   Object  (id=830061188504)   
mSelfObserverRegistered false   
mStackTraceElements null    
mUpdatedRows    HashMap  (id=830061144056)  
mWindow null    

And this clearly shows that the state of the Cursor is closed, as it should be - so does anyone have any clues as to why this should be throwing an exception?

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

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

发布评论

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

评论(1

沉默的熊 2024-09-13 04:32:47

而不是检查 c.getCount() > 0,检查c.moveToNext():

try{
...
    if (c != null && c.moveToNext())
    //do your thing here and don't call moveToFirst()
} finally {
    if (c != null)
    c.close();
}

并将关闭移动到finally{}

Instead of checking c.getCount() > 0, check for c.moveToNext():

try{
...
    if (c != null && c.moveToNext())
    //do your thing here and don't call moveToFirst()
} finally {
    if (c != null)
    c.close();
}

And move the close to finally{}

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