2012-07-06 176 views
0

我剛剛收到來自使用我的Android應用程序的用戶的錯誤報告。android.database.CursorIndexOutOfBoundsException:不知道爲什麼

java.lang.RuntimeException: Unable to start activity ComponentInfo{c`om.gurpswu.gurps/com.gurpswu.gurps.Home}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685) 
at android.app.ActivityThread.access$2300(ActivityThread.java:126) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4633) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84) 
at com.gurpswu.gurps.Player.getIntField(Player.java:137) 
at com.gurpswu.gurps.Home.hudSetup(Home.java:102) 
at com.gurpswu.gurps.Home.onCreate(Home.java:25) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633) 
... 11 more` 

我還沒有能夠重新創建問題。我希望你能幫助我。這是我的主屏幕上的代碼,從sqlite數據庫中檢索值。

public String getStringField(String fieldName) { 
     String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_CITY, 
       KEY_HEALTH, KEY_ENERGY, KEY_RANK, KEY_CASH, KEY_BALANCE, 
       KEY_DONATED }; 

     Cursor c = ourDatabase.query(true, DATABASE_TABLE, columns, null, null, 
       null, null, "_id DESC", "1"); 

     if (c != null) { 
      c.moveToFirst(); 
      String name = c.getString(c.getColumnIndexOrThrow(fieldName)); 
      c.close(); 
      return name; 
     } 
     return null; 
    } 

    public int getIntField(String fieldName) { 
     String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_CITY, 
       KEY_HEALTH, KEY_ENERGY, KEY_RANK, KEY_CASH, KEY_BALANCE, 
       KEY_DONATED }; 

     Cursor c = ourDatabase.query(true, DATABASE_TABLE, columns, null, null, 
       null, null, "_id DESC", "1"); 

     if (c != null) { 
      c.moveToFirst(); 
      int result = c.getInt(c.getColumnIndexOrThrow(fieldName)); 
      c.close(); 
      return result; 
     } 
     return (Integer) null; 
    } 
+1

嘗試'c.getCount = C的'0'instead = null'in你'if'聲明!。 – Barak 2012-07-06 23:35:45

回答

2
在堆棧跟蹤

,第一行你有CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0。做檢查c.getCount()c.moveToFirst()

if(c != null) { 
    try { 
     if (c.getCount() > 0) { 
      c.moveToFirst(); 
      return c.getInt(c.getColumnIndexOrThrow(fieldName)); 
     } 
    } 
    finally { 
     c.close(); 
    } 
} 
1

我的猜測是,你必須檢查的c.moveToFirst();的情況下返回值沒有任何記錄,這應該調用失敗(也許返回「falsy」值)

+0

到底! :)在堆棧跟蹤中,第一行有'CursorIndexOutOfBoundsException:索引0請求,大小爲0'。在'c.moveToFirst()'之前檢查'c.getCount()'# – 2012-07-06 23:37:41

0

如果什麼光標是空的 ?這個如果(C!= NULL):

更換這一點,如果(!C =空& & c.moveToFirst())

+0

@Barak如果由於某種原因,c確實爲null。他會得到一個空指針異常與您的建議! – Razvan 2012-07-06 23:38:17

+1

我不相信你需要測試遊標是否爲空。 [文檔](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#query%28java.lang.String,%20java.lang.String [],%20java.lang.String ,%20java.lang.String [],%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String%29)指出query()將返回「A Cursor對象,位於第一個條目之前「。它可能會返回一個_empty_光標,但是一個空的光標不爲空。 – Sam 2012-07-06 23:53:05