android.database.CursorIndexOutOfBoundsExcept: Index -1 requested, with a size of 1

阅读 64

2023-02-07



这个错误说明下标溢出,


例子:

Cursor cursor = getContentResolver().query(uri, new String[]{"contact_id"}, null, null, "contact_id desc limit 1"); //这有

一行数据

f(cursor !=null && cursor.getCount()>0){
int contact_id = cursor.getInt(0); //此时选择还在-1行,这里出错误
}

        改成:

f(cursor !=null && cursor.moveToFirst()){//直接指向第一行
int contact_id = cursor.getInt(0);
}

   





精彩评论(0)

0 0 举报