在我的应用程序中,我收到此错误,然后尝试解决该错误。但是我做不到。

Cursor c = G.db.getReceivedFromDatabaseByCount ( username, received_count );
if (c != null && c.moveToLast () && c.getCount () != 0) {
        for (int i = cursor_count; i > 0; i--) {
          ...
          c.moveToPrevious ();
        }
    }
c.close ();


在Google和此网站中进行任何搜索并将c.moveToLast ()添加到if后,必须解决此问题。

完整的LogCat结果:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample/com.sample.Service.ServiceDialog}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 4
    ...
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 4
    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.sample.Service.ServiceDialog.updateAndIntertDialog(ServiceDialog.java:216)
    at com.sample.Service.ServiceDialog.onCreate(ServiceDialog.java:125)
    at android.app.Activity.performCreate(Activity.java:5122)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
    ... 11 more
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 4
    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.sample.Service.ServiceDialog.updateAndIntertDialog(ServiceDialog.java:216)
    at com.sample.Service.ServiceDialog.onCreate(ServiceDialog.java:125)

最佳答案

您不应将int i = cursor_count设置为等同于游标的长度,该长度与上一个索引值不同。

应该是这样的:

for (int i = (cursor_count - 1); i >= 0; i--)

关于android - android database.CursorIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27254607/

10-10 23:29