Cursor code gives the error?
When i use the cursor.moveToNext(); in my code then the application Force finishing activity when i am scrolling down the bottom but when i am remove cursor.moveToNext(); it works normally ?
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));
}
}
Errors are
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/A开发者_高级运维ndroidRuntime(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)
when you get a Cursor returned from the database you should do an moveToFirst(); before trying to get data from it. try this
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));
}
Notice that the exception is being raised inside getNote
, not in populateForm
.
You should be checking the return code of moveToNext()
. If you're already past the end of the result set (i.e. there is no next
), it will return false
. In that case, you cannot access row data from that cursor (it effectively points nowhere). So you should not be calling getNote
on that cursor since it's trying to retrieve data from it.
精彩评论