2012-07-12 97 views
0

Apologies to those with bandwidth problems.Cursor.getInt()拋出CursorIndexOutOfBoundsException

所以,我有在SQLiteDatabaseHelper類讀取的數,並返回一個布爾值的方法,作爲上述圖爲。如圖所示,執行查詢通過ADB返回它就好

public boolean isTaskChecked(long taskID){ 
     SQLiteDatabase db = getReadableDatabase(); 
     String[] columns = {ID, CHECKED}; 
     Cursor c = db.query(TASKS, columns, "_id="+taskID, null, null, null, null); 
     c.moveToFirst(); 
     int boolAsInt = c.getInt(1); //This line is throwing. 
     //db.close(); 
     c.close(); 
     if (boolAsInt==1) { 
      return true; 
     } else if (boolAsInt==0) { 
      return false; 
     } else { 
      panicButton(); 
     } 
     return false; 
    } 

:但是,執行代碼只是拋出此異常:

06-28 19:49:45.881: E/AndroidRuntime(355): FATAL EXCEPTION: main 
06-28 19:49:45.881: E/AndroidRuntime(355): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84) 
06-28 19:49:45.881: E/AndroidRuntime(355): at com.mowdownDevelopments.nagTasks.NagTasksDatabaseHelper.isTaskChecked(NagTasksDatabaseHelper.java:239) 
06-28 19:49:45.881: E/AndroidRuntime(355): at com.mowdownDevelopments.nagTasks.NagTaskListFragment.onPrepareActionMode(NagTaskListFragment.java:184) 
06-28 19:49:45.881: E/AndroidRuntime(355): at com.actionbarsherlock.internal.ActionBarSherlockCompat$ActionModeCallbackWrapper.onPrepareActionMode(ActionBarSherlockCompat.java:1181) 
06-28 19:49:45.881: E/AndroidRuntime(355): at com.actionbarsherlock.internal.app.ActionBarImpl$ActionModeImpl.invalidate(ActionBarImpl.java:713) 
06-28 19:49:45.881: E/AndroidRuntime(355): at com.actionbarsherlock.internal.app.ActionBarImpl.startActionMode(ActionBarImpl.java:405) 
06-28 19:49:45.881: E/AndroidRuntime(355): at com.actionbarsherlock.internal.ActionBarSherlockCompat.startActionMode(ActionBarSherlockCompat.java:178) 
06-28 19:49:45.881: E/AndroidRuntime(355): at com.actionbarsherlock.app.SherlockFragmentActivity.startActionMode(SherlockFragmentActivity.java:50) 
06-28 19:49:45.881: E/AndroidRuntime(355): at com.mowdownDevelopments.nagTasks.NagTaskListFragment.onListItemClick(NagTaskListFragment.java:62) 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58) 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.widget.ListView.performItemClick(ListView.java:3382) 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.os.Handler.handleCallback(Handler.java:587) 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.os.Handler.dispatchMessage(Handler.java:92) 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.os.Looper.loop(Looper.java:123) 
06-28 19:49:45.881: E/AndroidRuntime(355): at android.app.ActivityThread.main(ActivityThread.java:4627) 
06-28 19:49:45.881: E/AndroidRuntime(355): at java.lang.reflect.Method.invokeNative(Native Method) 
06-28 19:49:45.881: E/AndroidRuntime(355): at java.lang.reflect.Method.invoke(Method.java:521) 
06-28 19:49:45.881: E/AndroidRuntime(355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
06-28 19:49:45.881: E/AndroidRuntime(355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
06-28 19:49:45.881: E/AndroidRuntime(355): at dalvik.system.NativeStart.main(Native Method) 

,其中指出的問題在這兒。 (我在那裏使用了一個泛型查詢,但select _id, ischecked from tasks where _id=1;也工作得很好。)我不明白爲什麼它說我在索引0時請求索引0,當代碼明確要求列索引1時,以及在執行在一個shell中的查詢給了我一些可行的東西(並且不是基於大小零的?)。任何人都可以幫我調試嗎?

[編輯:]所以,事實證明,問題是,對於這個特殊問題,我總是從不同的班級傳球-1。我很抱歉浪費大家的時間。

回答

3

這樣做:

if(c.moveToFirst()){ 
    int boolAsInt = c.getInt(1); 
} 

光標可能是空的,所以你必須想從它那裏得到任何值之前進行檢查。 moveToFirst()在光標爲空的情況下返回false,因此if將不會被輸入,並且不會再有異常

空遊標的原因是SQL查詢返回空結果集。如果你確定你應該再次獲得一個非空集檢查。

+0

我已經得到了使用中的SQL查詢相匹配的'rawquery'此錯誤這個問題,用一個數字代替變量。我不應該得到一個空集。 – MowDownJoe 2012-07-13 00:16:24

0

getInt方法的參數是指數,所以如果你想要得到查詢的第一列,你應該把0

相關問題