CursorIndexOutOfBoundsException problem:Index 4 requested, with a size of 4
I want to display for each maker an AlertDialog that has some description, for that, i'm using this code:
protected boolean onTap(int index) {
db = openHelper.getWritableDatabase();
String[] result_columns = new String[] {COL_DESCRI};
Cursor cur = db.query(true, TABLE_COORD, result_columns,
null, null, null, null, null, null);
cur.moveToPosition(index);
String description = cur.getString(cur.getColumnIndexOrThrow("description"));
AlertDialog.Builder dialog = new AlertDialog.Builder(Geo.this);
dialog.setTitle("Infos.");
dialog.setMessage(description);
dialog.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
//cur.moveToNext();
cur.close();
db.close();
return true;
}
The problem that there is some markers that the alertdialog result is correct, and there is some others that when i tap on it i have a FC. Logcat:
08-23 17:41:37.531: ERROR/AndroidRuntime(10004): Uncaught handler: thread main exiting due to uncaught exception
0开发者_开发技巧8-23 17:41:37.550: ERROR/AndroidRuntime(10004): android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at carburant.android.com.Geo$ItemizedOverlayPerso.onTap(Geo.java:244)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:346)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:506)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at com.google.android.maps.MapView.onTouchEvent(MapView.java:628)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.view.View.dispatchTouchEvent(View.java:3709)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.os.Looper.loop(Looper.java:123)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at java.lang.reflect.Method.invoke(Method.java:521)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-23 17:41:37.550: ERROR/AndroidRuntime(10004): at dalvik.system.NativeStart.main(Native Method)
When i put cur.moveToPosition(index);
under String description = cur.getString(cur.getColumnIndexOrThrow("description"));
i have Index -1 requested, with a size of 4
otherwise, like above code Index 4 requested, with a size of 4
Array indexing starts with 0
, not 1
. your fourth element is stored at index 3
.
When i put cur.moveToPosition(index); under String description = cur.getString(cur.getColumnIndexOrThrow("description")); i have Index -1 requested, with a size of 4 otherwise, like above code Index 4 requested, with a size of 4
When the row set is first returned the cursor will be at positon -1, which is before the first row.
just after calling
Cursor cur = db.query(true, TABLE_COORD, result_columns,
null, null, null, null, null, null);
your cursor will be at -1 position and this exception will come if you fetch data when cursor is on -1 index.
when you are calling cur.moveToPosition(index-1);
for the first time your index value may be 0 that is the issue.
The error is, certainly in code String description = cur.getString(cur.getColumnIndexOrThrow("description"));
because you have not got such column . You have only COL_DESCRI column, which probably does not match "description"
精彩评论