android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10244222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
提问by user181291
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
我正在使用自定义适配器扩展光标适配器在列表视图中显示数据,以显示特定的电话号码我已将 id 传递给数据库类中的一个方法,但它正在显示
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;
}
回答by Shubhayu
Whenever you are dealing with Cursors, ALWAYS check for null and check for moveToFirst()
without fail.
每当您处理游标时,始终检查 null 并确保moveToFirst()
无误。
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.
适当放置日志以查看它是返回null
还是空游标。根据那个检查你的查询。
UpdatePut both the checks in a single statement as mentioned by Jon in the comment below.
更新将两个检查放在一个语句中,正如 Jon 在下面的评论中提到的那样。
Update 2Put the close()
call within the valid cursor scope.
更新 2将close()
调用置于有效的游标范围内。
回答by ngesh
try this.. this will avoid an Exception being thrown when the cursor is empty..
试试这个..这将避免在游标为空时抛出异常..
if(cursor != null && cursor.moveToFirst()){
num = cursor.getString(cursor.getColumnIndex("ContactNumber"));
cursor.close();
}
回答by Khan
First check this Condition before fetching data
在获取数据之前首先检查此条件
if(cursor!=null && cursor.getCount()>0){
cursor.moveToFirst();
num = cursor.getString(cursor.getColumnIndex("ContactNumber"));
}
回答by Graham Borland
Check the return value from moveToFirst()
, before you try to read anything from the cursor. It looks as if no results are being returned.
moveToFirst()
在尝试从游标读取任何内容之前,请检查 , 的返回值。看起来好像没有返回任何结果。
回答by zapl
a save schema to query Cursor
s is
查询Cursor
s的保存模式是
// 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();
}
回答by Pouton Gerald
In case people are still looking:
如果人们仍在寻找:
Instead of searching for "ContactNumber"
try searching for Phone.NUMBER
. The tutorial has the code with more details: http://developer.android.com/training/basics/intents/result.html
而不是搜索"ContactNumber"
尝试搜索Phone.NUMBER
。该教程包含更多详细信息的代码:http: //developer.android.com/training/basics/intents/result.html
回答by Abhishek Dhyani
try to uninstall the app and then again test it... actually the sqlite db is created only once when the app is first install... so if you think your logic is current then reinstalling the app will do the trick for you. !!!!
尝试卸载该应用程序,然后再次对其进行测试...实际上,sqlite db 仅在首次安装该应用程序时创建一次...因此,如果您认为您的逻辑是最新的,那么重新安装该应用程序将为您解决问题。!!!!