gpt4 book ai didi

android - 光标崩溃 : Index 0 requested, 大小为 0

转载 作者:IT王子 更新时间:2023-10-29 06:30:17 26 4
gpt4 key购买 nike

我不知道为什么我的光标是空的。我阅读了包含相同信息的其他 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");
}
}

通过这段代码,我得到了 crach...请求了 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、hwVal : "34", "05/02/12", "hy,xsdhagsah"

最佳答案

只是说您的 Cursor 没有任何值,您仍然试图从 Cursor 读取数据。因此,最好检查 cursor.getCount();

的值
Log.d("Count",String.valueOf(cursor.getCount())); 
if(cursor.getCount() > 0){
// get values from cursor here
}

关于android - 光标崩溃 : Index 0 requested, 大小为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9158734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com