[英]Android cursor out of bounds exception index 0
我正在嘗試檢索數據庫中某人的姓名。 目前,我正在越界錯誤。
我知道我試圖檢索的數據存在,因為我有一個顯示數據庫中所有數據的列表視圖。
如果沒有檢查:
NameAppointPass.open();
String nameReturned = NameAppointPass.getName(sentID);
if(nameReturned != "")
{
Dialog d = new Dialog(this);
d.setTitle("returned");
TextView txt = new TextView(this);
txt.setText("win");
d.setContentView(txt);
d.show();
}
else
{
Dialog d = new Dialog(this);
d.setTitle("empty");
TextView txt = new TextView(this);
txt.setText("Fail");
d.setContentView(txt);
d.show();
}
希望有人可以看到我要去哪里。
目前,我遇到以下錯誤:
01-21 21:41:43.753: E/AndroidRuntime(276): FATAL EXCEPTION: main
01-21 21:41:43.753: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.addAppointment}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
我正在使用onclick的“長變量”從列表視圖發送所需數據的ID,如下所示:
public void onListItemClick(ListView list, View v, int list_posistion, long item_id)
{
long idToPass = item_id;
Intent setName = new Intent("com.example.flybase2.addAppointment");
setName.putExtra("newPassedID", idToPass);
startActivity(setName);
}
然后通過捆綁將其發送到對象實例'NameAppointPass',如下所示:
NameAppointPass.open();
String nameReturned = NameAppointPass.getName(sentID);
以下代碼顯示了查詢數據庫的方法getName
,該方法應返回存儲在選定ID處的名稱,但我收到了錯誤消息:
public String getName(long passedID) {
String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP};
Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, KEY_ROWAPPID + "=" + passedID, null, null, null, null);
if(c != null)
{
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
如果游標可以為空,則可以檢查游標是否沒有數據:
if(c != null && c.getCount() > 0)
為了進一步診斷為什么游標為空,請在各個位置放置日志語句以檢查項目ID是否仍然正確。
設法得到排序。 最后,我為保存傳遞的ID錯誤地將變量命名錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.