【问题标题】:android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为 0
【发布时间】:2012-05-01 22:05:40
【问题描述】:

我正在使用自定义适配器扩展光标适配器在列表视图中显示数据,以显示特定的电话号码,我已将 id 传递给数据库类中的方法,但它正在显示

errorandroid.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 

在将调试器放在方法中时,它不会在行之后

num = cursor.getString(cursor.getColumnIndex("ContactNumber"));

谁能帮我解决。 这是代码:

public String getNumberFromId(int id) 
{
    String num;
    db= this.getReadableDatabase();
    Cursor cursor = db.query(scheduletable, new String[] { "ContactNumber" },"_id="+id, null, null, null, null);
    cursor.moveToFirst();
    num = cursor.getString(cursor.getColumnIndex("ContactNumber")); 
    cursor.close();
    db.close();
    return num;
}

【问题讨论】:

    标签: android database-cursor


    【解决方案1】:

    每当您处理游标时,请始终检查 null 并检查 moveToFirst() 不会失败。

    if( cursor != null && cursor.moveToFirst() ){
        num = cursor.getString(cursor.getColumnIndex("ContactNumber"));
        cursor.close(); 
    }
    

    适当地放置日志以查看它是返回null 还是空游标。根据该检查您的查询。

    更新将两个检查放在一个语句中,正如 Jon 在下面的评论中提到的那样。

    更新 2close() 调用置于有效游标范围内。

    【讨论】:

    • 错了,如果 cursors 为 null 并且你关闭了这个 cursor,你就变成了 NPE
    • 我不知道我当时是怎么错过的。接得好。谢谢!
    • 这段代码对我有用...我确实想要那个。
    • 如果cursor != null 为真,而cursor.movetofirst() 为假,则无法访问cursor.close()。在这种情况下,您似乎仍想关闭光标,这是不好的做法?除非moveToFirst() 总是返回true?
    【解决方案2】:

    试试这个..这将避免在光标为空时抛出异常..

    if(cursor != null && cursor.moveToFirst()){
        num = cursor.getString(cursor.getColumnIndex("ContactNumber")); 
        cursor.close();
    }
    

    【讨论】:

    • 这段代码对我有用...我确实想要那个。
    • 如果 cursor 为空,此代码将在 cursor.close() 上崩溃。
    • @AkashAgarwal 类似于检查计数是否大于零。只需确保光标也不为空。 if(cursor != null && cursor.getCount()!= 0)
    【解决方案3】:

    取数据前先检查这个Condition

    if(cursor!=null && cursor.getCount()>0){
      cursor.moveToFirst();
      num = cursor.getString(cursor.getColumnIndex("ContactNumber")); 
    }
    

    【讨论】:

      【解决方案4】:

      在尝试从光标读取任何内容之前,请检查 moveToFirst() 的返回值。看起来好像没有返回任何结果。

      【讨论】:

        【解决方案5】:

        用于查询Cursors 的保存模式是

        // just one
        Cursor cursor = db.query(...);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                value = cursor.getSomething();
            }
            cursor.close();
        }
        
        // multiple columns
        Cursor cursor = db.query(...);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                values.add(cursor.getSomething());
            }
            cursor.close();
        }
        

        【讨论】:

          【解决方案6】:

          如果人们还在寻找:

          尝试搜索Phone.NUMBER,而不是搜索"ContactNumber"。本教程有更详细的代码:http://developer.android.com/training/basics/intents/result.html

          【讨论】:

            【解决方案7】:

            尝试卸载该应用程序,然后再次对其进行测试...实际上 sqlite db 仅在首次安装应用程序时创建一次...因此,如果您认为您的逻辑是最新的,那么重新安装应用程序就可以解决问题你。 !!!!

            【讨论】:

              猜你喜欢
              • 2014-09-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-06-01
              • 2013-08-02
              • 2016-02-09
              • 1970-01-01
              相关资源
              最近更新 更多