 |
BOOK: Professional Android 2 Application Development  | This is the forum to discuss the Wrox book Professional Android 2 Application Development, 2nd Edition by Reto Meier; ISBN: 978-0-470-56552-0 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional Android 2 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 16th, 2010, 04:35 PM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 13
Thanks: 1
Thanked 2 Times in 2 Posts
|
|
Fix: Chapter 7 - Earthquake IllegalStateException Finalizing cursor
Hi,
The debugger was suspending the Ch. 7 Earthquake app due to an IllegalStateException. The error from LogCat:
Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c5db08 on earthquakes that has not been deactivated or closed
I traced the problem to this line in Earthquake.addNewQuake(...):
Code:
if (cr.query(EarthquakeProvider.CONTENT_URI, null, where, null, null).getCount() == 0) {
cr.query opens a new cursor, but it's not being properly closed.
Here is how I worked around the problem:
Code:
Cursor cursor = cr.query(EarthquakeProvider.CONTENT_URI, null, where, null, null);
int count = cursor.getCount();
cursor.close();
if (count == 0) {
|
|

August 17th, 2010, 10:06 AM
|
|
Authorized User
|
|
Join Date: Jul 2010
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Tried fix - got 2 instances of Error instead of 6
I tried this fix, it reduced the occurrences of the error from 6 to 2 but does not seem to have fully eliminated the problem when I run it. Is there another instance in another activity where the cursor has to be closed?
This is the error I get 2 instances of:
ERROR/Cursor(431): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com.paad.earthquake/databases/earthquakes.db, table = earthquakes, query = SELECT * FROM earthquakes ORDER BY date
ERROR/Cursor(431):android.database.sqlite.DatabaseObject NotClosedException: Application did not close the cursor or database object that was opened here
|
|

August 17th, 2010, 12:53 PM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 13
Thanks: 1
Thanked 2 Times in 2 Posts
|
|
also close cursor in loadQuakesFromProvider
Hi MellowFellow,
Add c.close() as the last line of loadQuakesFromProvider():
Code:
} while(c.moveToNext());
}
c.close();
}
|
|
The Following User Says Thank You to juniper For This Useful Post:
|
|
|

August 19th, 2011, 03:25 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by juniper
Hi,
The debugger was suspending the Ch. 7 Earthquake app due to an IllegalStateException. The error from LogCat:
Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@44c5db08 on earthquakes that has not been deactivated or closed
I traced the problem to this line in Earthquake.addNewQuake(...):
Code:
if (cr.query(EarthquakeProvider.CONTENT_URI, null, where, null, null).getCount() == 0) {
cr.query opens a new cursor, but it's not being properly closed.
Here is how I worked around the problem:
Code:
Cursor cursor = cr.query(EarthquakeProvider.CONTENT_URI, null, where, null, null);
int count = cursor.getCount();
cursor.close();
if (count == 0) {
|
try this
Code:
private void addNewQuake(Quake _quake) {
ContentResolver cr = getContentResolver();
// Construct a where clause to make sure we donÃt already have this
// earthquake in the provider.
String w = EarthquakeProvider.KEY_DATE + " = " + _quake.getDate().getTime();
// If the earthquake is new, insert it into the provider.
Cursor c = cr.query(EarthquakeProvider.CONTENT_URI, null, w, null, null);
if (c.getCount()==0){
ContentValues values = new ContentValues();
values.put(EarthquakeProvider.KEY_DATE, _quake.getDate().getTime());
values.put(EarthquakeProvider.KEY_DETAILS, _quake.getDetails());
double lat = _quake.getLocation().getLatitude();
double lng = _quake.getLocation().getLongitude();
values.put(EarthquakeProvider.KEY_LOCATION_LAT, lat);
values.put(EarthquakeProvider.KEY_LOCATION_LNG, lng);
values.put(EarthquakeProvider.KEY_LINK, _quake.getLink());
values.put(EarthquakeProvider.KEY_MAGNITUDE, _quake.getMagnitude());
cr.insert(EarthquakeProvider.CONTENT_URI, values);
announceNewQuake(_quake);
}
c.close();
}
|
|
 |