IndexOutOfBoundsException when I call stopManagingCursor in an OnItemSelectedListener

by Jeffrey Yasskin » Sun, 08 Mar 2009 08:50:37 GMT

Sponsored Links
 Activity.performStop() contains the following loop (around line 3394
in the 1.0 SDK release and at ;a=blob;f=core/java/android/app/;h=4dc4b6a48b85f9caad4234b8e29fafdc9260840c;hb=HEAD#l3504):

            final int N = mManagedCursors.size();
            for (int i=0; i<N; i++) {
                ManagedCursor mc = mManagedCursors.get(i);
                if (!mc.mReleased) {
                    mc.mReleased = true;

In my app, mc.mCursor.deactivate() calls (indirectly)
SimpleCursorAdapter.notifyDataSetInvalidated(), which calls
(indirectly) AdapterView.fireOnSelected(), which calls my
OnItemSelectedListener, which calls stopManagingCursor(), which
reduces the size of mManagedCursors, which makes the ArrayList.get()
call throw on the next iteration.

I'm calling stopManagingCursor to avoid the crash described at <http://
f6fabc180e8b517a/c04691d80f1e9135?lnk=gst&#c04691d80f1e9135> (and a
few other posts you can find by searching this group for

I suspect this is a bug in Activity.performStop, which shouldn't
assume mManagedCursors is constant through a call to a user-provided
callback. Until the Android developers can fix the bug, I'm looking
for a workaround. Should I just abandon managed cursors, which don't
seem to work well with CursorAdapters? Is there a good way to prevent
my OnItemSelectedListener from being called during performStop()? Any
other ideas?


IndexOutOfBoundsException when I call stopManagingCursor in an OnItemSelectedListener

by Jeffrey Yasskin » Sun, 08 Mar 2009 10:40:05 GMT

 That's a reasonable idea, thanks. Right now, I'm calling changeCursor
(null) for each of my adapters in onDestroy(), but if I run into
trouble with leaking cursors before I can set them into an adapter
I'll try removing the listeners.

Filed at 


Sponsored Links

Other Threads

1. why the alertdialog could not display ?

I am trying to display the dialog,but something is wrong when I do
it.I use a timer to do something. I want to use a dialog to let the
user konw, when the dialog  is end.
seekTask = new TimerTask()
new AlertDialog.Builder(mContext)
                .setPositiveButton(R.string.btn_ok, new
                        public void onClick(DialogInterface d, int which)

//Log.i("tag","message")  the log could not display.


2. What is really locked in the HTC Magic?

Been thinkin of getting a device to get my apps on the actual road. I have
the Dev Phone option (although its not shipped to Portugal..thanks google by
the way...), but also the HTC Magic and soon the Samsung Galaxy. The Magic
is locked, but what this really means? Only the SIM is locked? Do i have all
the functionalities and developing opportunities as in the Dev Phone? Or is
it hardware-locked? A little help from you device developers would be



Hugo Rafael Augusto


3. Spinner add values at run tim

4. Embedded webview crashes on dialog

5. Filter out contacts from contacts pick list || option to create new contact from pick list

6. Application Resources on SDCARD on install!! MUST HAVE, GOOGLE DO SOMETHING!!

7. Pin-Entr