我不知道为什么我的光标是空的。我读了其他4-5个问题,其中包含相同的信息,但我无法解决我的问题。我忘记了什么?
使用此代码我得到消息:“光标为空”:
public void onClick(View arg0) {
String txt_edit_hw = edit_hw.getText().toString();
ContentValues args = new ContentValues();
args.put("hw", txt_edit_hw);
String test = "SELECT _id FROM tbl_homework where hw='"+ txt_edit_hw +"';";
Cursor mCursor = db.rawQuery(test, null);
startManagingCursor(mCursor);
if (mCursor.moveToFirst()){
String hw = mCursor.getString(mCursor.getColumnIndex("hw"));
doMessage(hw);
db.update("tbl_homework", args, "_id=34", null);
fillData();
} else {
doMessage("Cursor is empty");
}
}
使用此代码我得到了...请求android索引0,大小为0:
public void onClick(View arg0) {
String txt_edit_hw = edit_hw.getText().toString();
ContentValues args = new ContentValues();
args.put("hw", txt_edit_hw);
String test = "SELECT _id FROM tbl_homework where hw='"+ txt_edit_hw +"';";
Cursor mCursor = db.rawQuery(test, null);
startManagingCursor(mCursor);
mCursor.moveToFirst();
String hw = mCursor.getString(mCursor.getColumnIndex("hw"));
doMessage(hw);
db.update("tbl_homework", args, "_id=34", null);
fillData();
}
这种方式出现同样的错误:它崩溃了(我评论过它)
public void onClick(View arg0) {
String txt_edit_hw = edit_hw.getText().toString();
txt_edit_hw.trim();
ContentValues args = new ContentValues();
args.put("hw", txt_edit_hw);
String test = "SELECT _id FROM tbl_homework where hw='"+ txt_edit_hw +"';";
Cursor mCursor = db.rawQuery(test, null);
startManagingCursor(mCursor);
mCursor.moveToFirst();
/**It crashes here**/String hwl = mCursor.getString(mCursor.getColumnIndex("_id"));
doMessage(hwl);
db.update("tbl_homework", args, "_id=34", null);
fillData();
}
02-06 12:13:49.789: I/Process(6112): Sending signal. PID: 6112 SIG: 9
02-06 12:13:55.582: D/dalvikvm(6140): GC_EXTERNAL_ALLOC freed 37K, 49% free 2785K/5379K, external 5211K/5218K, paused 23ms
02-06 12:13:58.785: D/AndroidRuntime(6140): Shutting down VM
02-06 12:13:58.785: W/dalvikvm(6140): threadid=1: thread exiting with uncaught exception (group=0x40091568)
02-06 12:13:58.796: E/AndroidRuntime(6140): FATAL EXCEPTION: main
02-06 12:13:58.796: E/AndroidRuntime(6140): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-06 12:13:58.796: E/AndroidRuntime(6140): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
02-06 12:13:58.796: E/AndroidRuntime(6140): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
02-06 12:13:58.796: E/AndroidRuntime(6140): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
02-06 12:13:58.796: E/AndroidRuntime(6140): at test.marco.notenha.homework$3.onClick(homework.java:227)
02-06 12:13:58.796: E/AndroidRuntime(6140): at android.view.View.performClick(View.java:2486)
02-06 12:13:58.796: E/AndroidRuntime(6140): at android.view.View$PerformClick.run(View.java:9130)
02-06 12:13:58.796: E/AndroidRuntime(6140): at android.os.Handler.handleCallback(Handler.java:587)
02-06 12:13:58.796: E/AndroidRuntime(6140): at android.os.Handler.dispatchMessage(Handler.java:92)
02-06 12:13:58.796: E/AndroidRuntime(6140): at android.os.Looper.loop(Looper.java:130)
02-06 12:13:58.796: E/AndroidRuntime(6140): at android.app.ActivityThread.main(ActivityThread.java:3703)
02-06 12:13:58.796: E/AndroidRuntime(6140): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 12:13:58.796: E/AndroidRuntime(6140): at java.lang.reflect.Method.invoke(Method.java:507)
02-06 12:13:58.796: E/AndroidRuntime(6140): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-06 12:13:58.796: E/AndroidRuntime(6140): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-06 12:13:58.796: E/AndroidRuntime(6140): at dalvik.system.NativeStart.main(Native Method)
表: 行:_id,hwdate,hw Val:“34”,“05/02/12”,“hy,xsdhagsah”
答案 0 :(得分:15)
简单地说,您的Cursor
没有任何价值,但您仍尝试阅读Cursor
。因此,最好检查cursor.getCount();
Log.d("Count",String.valueOf(cursor.getCount()));
if(cursor.getCount() > 0){
// get values from cursor here
}
答案 1 :(得分:3)
您选择_id
列,但使用mCursor.getColumnIndex("hw")
。您的选择中没有列hw
,只有_id
。 getColumnIndex()
返回0
,您会收到相应的错误。
UPD:无需使用getCount()
。让我分享一些代码片段。首先,我们假设我们只需要光标中的第一个值:
Cursor cursor = //query here
if (cursor.moveToFirst()) {
// retrieve values from the cursor
}
如果我们需要多个值:
Cursor cursor = //query here
while (cursor.moveToNext()) {
// retrieve next set of values from the cursor
}
光标的初始位置始终为-1
,这就是上述代码可以工作并处理所有条目的原因。