getContentResolver query cause CursorWrapperInner warning

后端 未结 2 817
自闭症患者
自闭症患者 2020-12-19 09:01

On 4.0.3, Code below cause warning \"W/CursorWrapperInner(11252): Cursor finalized without prior close()\".

    Uri uri = Uri.withAppendedPath(PhoneLookup.CO         


        
相关标签:
2条回答
  • 2020-12-19 09:16

    This line of code returns a Cursor object:

    getContentResolver().query(uri, null, null, null, null);
    

    It's odd that you are performing a query but ignoring the result. The only purpose of performing a query is to get the results in a Cursor. You should store that into a variable like so:

    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    

    Then you can use the cursor to get whatever data you need and when you are done with it call:

    cursor.close();
    

    You can close the cursor in Activity#onDestroy() or earlier, but you must close it before the Activity is completely finished or you will see this warning. This is because a Cursor is backed by memory in another process and you don't want to leak that memory.

    0 讨论(0)
  • 2020-12-19 09:39

    I have also experienced this weird issue. I'm using a ContentProvider to provide me with a Cursor and a CursorLoader to handle the fetching in my Fragment/Activities. So I'm doing everything "to the book".

    I experienced this warning message using a 4.1.1 device, but it seems to have gone away on my Nexus 7 which is 4.2. Personally I would not take any serious notice of this warning.

    Update: I was testing my code with Android v2.2 and I got a full stacktrace from this error. It turned out that my code was fetching another Cursor (not the one fetched using a Loader) and it was this that was the offending code. Closing this manually did the trick.

    0 讨论(0)
提交回复
热议问题