我有这段代码在贴上标记时显示AlertDialog:
public void showOverlay (OverlayItem overlay)
{
db = openHelper.getWritableDatabase();
String[] columns_descri = new String[] {COL_DESCRI};
Cursor cur = db.query(true, TABLE_COORD, columns_descri,null, null, null, null, null, null);
String description = cur.getString(cur.getColumnIndexOrThrow(COL_DESCRI));
AlertDialog.Builder dialog = new AlertDialog.Builder(Geo.this);
dialog.setTitle("Infos.");
dialog.setMessage(description);
dialog.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
db.close();
}
@Override
public boolean onTap(int index)
{
showOverlay(getItem(index)) ;
return super.onTap(index) ;
}
奇怪的是我有一个带有logcat错误的FC:
08-24 20:51:42.466: ERROR/AndroidRuntime(265): Uncaught handler: thread main exiting due to uncaught exception
08-24 20:51:42.486: ERROR/AndroidRuntime(265): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 4
请问好吗? 谢谢。
答案 0 :(得分:1)
首次创建光标时,它的索引位于-1(在列表开头之前)。在尝试从中检索任何数据之前,您需要调用cur.moveToFirst()
。如果光标为空,moveToFirst()
也将返回false,这可能很有用:
public void showOverlay (OverlayItem overlay, int index)
{
db = openHelper.getWritableDatabase();
String[] columns_descri = new String[] {COL_DESCRI};
Cursor cur = db.query(true, TABLE_COORD, columns_descri,null, null, null, null, null, null);
if (cur.moveToPosition(index)) {
String description = cur.getString(cur.getColumnIndexOrThrow(COL_DESCRI));
AlertDialog.Builder dialog = new AlertDialog.Builder(Geo.this);
dialog.setTitle("Infos.");
dialog.setMessage(description);
dialog.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
else {
//ERROR! cursor is empty, throw a toast or something
}
db.close();
}