光标代码给出了错误?

时间:2011-10-01 07:18:37

标签: android

当我使用cursor.moveToNext();在我的代码然后应用程序强制完成活动时,我向下滚动底部,但当我删除cursor.moveToNext();它正常工作?

class NoteHolder {
private Button b1 = null;
private Button b2 = null;
private Button b3 = null;


NoteHolder(View row) {
    b1 = (Button) row.findViewById(R.id.one);
    b2 = (Button) row.findViewById(R.id.two);
    b3 = (Button) row.findViewById(R.id.three);

}

void populateFrom(Cursor c, NoteHelper helper) {
    b1.setText(helper.getNote(c));
        c.moveToNext();
    b2.setText(helper.getNote(c));
    c.moveToNext();
    b3.setText(helper.getNote(c));
}

}

错误是

    ERROR/AndroidRuntime(622): FATAL EXCEPTION: main
ERROR/AndroidRuntime(622): android.database.CursorIndexOutOfBoundsException: Index 16 requested, with a size of 16
ERROR/AndroidRuntime(622):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
 ERROR/AndroidRuntime(622):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
ERROR/AndroidRuntime(622):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
ERROR/AndroidRuntime(622):     at com.example.buttontest.ButtonTest$NoteHelper.getNote(ButtonTest.java:141)
ERROR/AndroidRuntime(622):     at com.example.buttontest.ButtonTest$NoteHolder.populateFrom(ButtonTest.java:105)
ERROR/AndroidRuntime(622):     at com.example.buttontest.ButtonTest$NoteAdapter.bindView(ButtonTest.java:71)
 ERROR/AndroidRuntime(622):     at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
ERROR/AndroidRuntime(622):     at android.widget.AbsListView.obtainView(AbsListView.java:1294)
ERROR/AndroidRuntime(622):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
ERROR/AndroidRuntime(622):     at android.widget.ListView.fillDown(ListView.java:652)
ERROR/AndroidRuntime(622):     at android.widget.ListView.fillGap(ListView.java:623)
ERROR/AndroidRuntime(622):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
ERROR/AndroidRuntime(622):     at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:2485)
ERROR/AndroidRuntime(622):     at android.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroidRuntime(622):     at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroidRuntime(622):     at android.os.Looper.loop(Looper.java:123)
 ERROR/AndroidRuntime(622):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 ERROR/AndroidRuntime(622):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(622):     at java.lang.reflect.Method.invoke(Method.java:521)
 ERROR/AndroidRuntime(622):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(622):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(622):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

当你从数据库中返回一个Cursor时,你应该做一个moveToFirst();在尝试从中获取数据之前。试试这个

void populateFrom(Cursor c, NoteHelper helper) {
    if (!c.moveToFirst()) return; //if no data dont do anything
    b1.setText(helper.getNote(c));
        c.moveToNext();
    b2.setText(helper.getNote(c));
    c.moveToNext();
    b3.setText(helper.getNote(c));
}

答案 1 :(得分:0)

请注意,异常是在getNote内引发的,而不是populateForm

您应该检查moveToNext()的返回代码。如果您已经超过结果集的末尾(即没有next),则会返回false。在这种情况下,您无法从该光标访问行数据(它实际上无法指向)。因此,您不应该在该游标上调用getNote,因为它正在尝试从中检索数据。