android.database.CursorIndexOutOfBoundsException:指数0要求,为0的大小 [英] android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

查看:218
本文介绍了android.database.CursorIndexOutOfBoundsException:指数0要求,为0的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的列表视图中显示数据扩展游标接口定义适配器,可以显示特定的电话号码,我已经通过了ID在数据库类中的方法却是露出

  errorandroid.database.CursorIndexOutOfBoundsException:指数0要求,为0的大小
 

,同时将调试器的方法是不是行之后将

  NUM = cursor.getString(cursor.getColumnIndex(ContactNumber));
 

任何一个可以帮助我解决这个问题。 这是code:

 公共字符串getNumberFromId(INT ID)
{
    字符串NUM;
    DB = this.getReadableDatabase();
    光标光标= db.query(scheduletable,新的String [] {ContactNumber},_ ID =+ ID,NULL,NULL,NULL,NULL);
    cursor.moveToFirst();
    NUM = cursor.getString(cursor.getColumnIndex(ContactNumber));
    cursor.close();
    db.close();
    返回NUM;
}
 

解决方案

当你正在处理光标,总是检查空和检查moveToFirst()没有失败。

 如果(光标= NULL和放大器;!&安培; cursor.moveToFirst()){
    NUM = cursor.getString(cursor.getColumnIndex(ContactNumber));
    cursor.close();
}
 

将适当记录,看看它是否返回null或一个空指针。根据该检查查询。

更新将同时在一条语句,如下面的评论中提及了由Jon检查。

更新2 把close()方法的有效光标范围内调用。

I am using custom adapter extending cursor adapter for displaying data in listview, to display particular phone number i have passed the id to a method in database class but it is showing

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

while placing debugger in the the method it is not going after the line

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

Can any one help me to solve it. This is the code:

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;
}

解决方案

Whenever you are dealing with Cursors, ALWAYS check for null and check for moveToFirst() without fail.

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

Place logs appropriately to see whether it is returning null or an empty cursor. According to that check your query.

Update Put both the checks in a single statement as mentioned by Jon in the comment below.

Update 2 Put the close() call within the valid cursor scope.

这篇关于android.database.CursorIndexOutOfBoundsException:指数0要求,为0的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆