我是 Android 新手,并试图从这样的数据库中选择一行:
public Cursor getEntry(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor entry = db.query(TABLE_NAME, new String[]{
"id", "title", "description", "image_path"
}, "id" + "=?", new String[]{ String.valueOf(id) }, null, null, null, null);
return entry;
}
并尝试像这样输出它:
DatabaseHelper db = new DatabaseHelper(this);
TextView tv = new TextView(this);
if (db.getEntry(1) != null){
while (db.getEntry(1).moveToFirst()){
tv.setText("" + db.getEntry(1).getString(1));
}
}
setContentView(tv);
和错误:
ERROR/AndroidRuntime(1850): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.untitled1/com.example.untitled1.DisplayMessageActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.untitled1.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
问题是什么?
谢谢