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
 http://android.git.kernel.org/?p=platform/frameworks/base.git ;a=blob;f=core/java/android/app/Activity.java;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.mCursor.deactivate();
                    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://
groups.google.com/group/android-developers/browse_thread/thread/
f6fabc180e8b517a/c04691d80f1e9135?lnk=gst&#c04691d80f1e9135> (and a
few other posts you can find by searching this group for
"SQLiteClosable").

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?

Thanks,
Jeffrey
--~--~---------~--~----~------------~-------~--~----~



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  http://code.google.com/p/android/issues/detail?id=2174. 




--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. When Activity is closed, its fields is not release, how to resolve this issue.

Thanks.

Even if I called System.gc(), the 'obj' field of MyActivity is still not 
released.

If I don't use infinite loop, and tried the following test, exception 'out of 
memory' still occurs
1, Start Activity A
2, Press a button on Activity A to start Activity B
3, In onCreate method of B, allocate 5MB, then assign it to a field, this field 
is not released explicitly in onDestroy of B
4, Press "Back" key to return to A,
5, Execute System.gc in A, then start B again
6, Repeat steps 4 and 5 about 4 times, exception "out of memory" occurs

--- 100, Mark Murphy <mmur...@commonsware.com> 
 Mark Murphy <mmur...@commonsware.com>
: Re: [android-developers] When Activity is closed, its fields is not   
release, how to resolve this issue.
 android-developers@googlegroups.com
: 20100,8:17



I am going to assume that by "not released", you mean "not garbage
collected".


If something in your code is keeping your instance of MyActivity in memory
-- such as holding onto an instance of it or something referring to it in
a static context -- then you will have a memory leak.


You are creating an infinite loop. Activity A be called with onResume() as
part of Activity B finishing, so you are restarting Activity B right away.
Do not create an infinite loop, and you will not run out of memory.

There are tools in Android to diagnose true memory leaks, such as the
Allocation Tracker and VM Heap in DDMS, or the dump HPROF file button in
DDMS in Android 2.x. Those will help you track down your memory leak.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com
Android App Developer Books: http://commonsware.com/books.html


-- 

2. how to get inputstream of an xml file which is placed under /res/xml




Don't think you can. For those files, use getXml() (or something) to get an
XmlPullParser instance (I think, I'm going off memory).

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices
http://sites.google.com/site/rezmobileapps/treking

-- 

3. Turning off the screen

4. OOT: Some Humor from Bill Gates and Steve Jobs

5. Gmail validasi lagi...

6. HTC EVO 4G gratis

7. froyo vs eclair vs ipad: Android Froyo Running Laps Around The iPad 鈥?Literally