我希望從sqlite數據庫獲取單行。從基於id的數據庫中獲取單行導致錯誤
我使用下面的代碼來獲取值。
public ShipmentStatusDao getShipmentStatusRow(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SHIPMENTSTATUS, new String[]{KEY_NOTES,
KEY_PHOTOS, KEY_DOCUMENTS}, KEY_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
ShipmentStatusDao notification = new ShipmentStatusDao(cursor.getInt(0),
cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8));
return notification;
}
進出口使用以下代碼來調用上述方法,
ShipmentStatusDao shipmentStatusRow = db.getShipmentStatusRow(1);
shipmentStatusRow.getDate();
林得到以下錯誤,
java.lang.IllegalStateException:無法讀取行0,來自 CursorWindow的列3。確保光標在 從其訪問數據之前正確初始化。 在android.database.CursorWindow.nativeGetString(本機方法) 在android.database.CursorWindow.getString(CursorWindow.java:438) 在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 在com.vaighai .handlers.NotificationDbHandler.getShipmentStatusRow在(NotificationDbHandler.java:364) 在com.vaighai.fragments.StatusFragment $ 5.onItemClick(StatusFragment.java:237) 在android.widget.AdapterView.performItemClick(AdapterView.java:301) android.widget.AbsListView.performItemClick(AbsListView.java:1510) 在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3339) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) at dalvik.system.NativeStart.main(Native方法)03-16 14:54:32.826 2337-7411 /? I/ActivityManager:通知應用程序崩潰
我該如何才能糾正上述錯誤?
'如果(指針!= NULL){//你的代碼}'喜歡這種改變將幫助你 – Sree