首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android SQLiteOpenHelper

Android SQLiteOpenHelper
EN

Stack Overflow用户
提问于 2014-09-24 10:56:51
回答 1查看 82关注 0票数 0

我的应用程序崩溃

代码语言:javascript
复制
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}:
    java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
    ...

下面是导致错误的方法:

代码语言:javascript
复制
public int getWeek(int yearAndWeek) throws CursorIndexOutOfBoundsException {
    SQLiteDatabase db = this.getReadableDatabase();

    String selectQuery = "SELECT  * FROM " + TABLE_WEEKS + " WHERE "
            + KEY_WEEK + " = " + yearAndWeek;

    Cursor c = db.rawQuery(selectQuery, null);

    if (c.getCount() > 0)
        c.moveToFirst();

    return c.getInt(c.getColumnIndex(KEY_WEEK_NUMBER));
}

此错误发生在这行return c.getInt(c.getColumnIndex(KEY_WEEK_NUMBER));中。请帮我解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-24 11:03:31

要从方法中保持正确的答复,您可以尝试使用以下代码。

代码语言:javascript
复制
public int getWeek(int yearAndWeek) throws CursorIndexOutOfBoundsException {

    SQLiteDatabase db = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM " + TABLE_WEEKS + " WHERE " + KEY_WEEK + " = " + yearAndWeek;
    Cursor c = db.rawQuery(selectQuery, null);

    if (c.getCount() > 0) {
        c.moveToFirst();
        return c.getInt(c.getColumnIndex(KEY_WEEK_NUMBER));
    }
    return -1;        
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26015144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档