Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Professional Android 2 Application Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 16th, 2010, 04:35 PM
Registered User
 
Join Date: Aug 2010
Posts: 13
Thanks: 1
Thanked 2 Times in 2 Posts
Default 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) {
Reply With Quote
  #2 (permalink)  
Old August 17th, 2010, 10:06 AM
Authorized User
 
Join Date: Jul 2010
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #3 (permalink)  
Old August 17th, 2010, 12:53 PM
Registered User
 
Join Date: Aug 2010
Posts: 13
Thanks: 1
Thanked 2 Times in 2 Posts
Default also close cursor in loadQuakesFromProvider

Hi MellowFellow,

Add c.close() as the last line of loadQuakesFromProvider():
Code:
	} while(c.moveToNext());
      }
      c.close();
    }
Reply With Quote
The Following User Says Thank You to juniper For This Useful Post:
MellowFellow (August 17th, 2010)
  #4 (permalink)  
Old August 19th, 2011, 03:25 AM
Registered User
 
Join Date: Aug 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by juniper View Post
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();
  }
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 7 Earthquake 2, Earthquake.java, error onActivityResult dbomberg BOOK: Professional Android 2 Application Development 2 November 30th, 2012 05:52 AM
Please help: Chapter 9 Earthquake 4 dahe BOOK: Professional Android 2 Application Development 0 July 21st, 2010 03:41 PM
chapter 5 Earthquake EricTapia BOOK: Professional Android 2 Application Development 2 April 14th, 2010 12:47 AM
Chapter 6 Earthquake using Provider abowman BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 0 March 9th, 2009 05:19 PM
Chapter 6 Sample Earthquake 2 sunrain BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 2 January 31st, 2009 04:06 PM



All times are GMT -4. The time now is 11:59 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.