Thank you, adding cursor.close(); fixed the problem.

On Tue, Jun 14, 2011 at 8:06 PM, Kostya Vasilyev <[email protected]> wrote:

> When you see Eclipse stuck like this, just press Resume a few times, to let
> the application crash (or recover) completely. Then check the logcat for a
> message and a stack trace.
>
> In this particular case, the code in getImgResource leaves the cursor open,
> so there is a warning when it's finalized.
>
> This is the pattern I use:
>
> cursor = ... query ....
> if (cursor != null) {
>     .... get column indices ...
>     while (cursor.moveToNext()) {
>             ... get data using the column indices .....
>     }
>     cursor.close();
> }
>
> This works because a cursor's initial position is -1 (before the first
> record).
>
> -- Kostya
>
> 2011/6/14 Simon Platten <[email protected]>
>
>> I have written a class 'clsDB', please see source below.  I'm testing it
>> in the emulator and the first time around it runs fine without error, then
>> on the second run eclipse jumps into the Class File Editor for
>> SQLiteCursor.class.  The application continues to run without exception or
>> error, but eclipse suggests everything isn't quite right.
>>
>> I have try / catch clauses everywhere I interact with the database and
>> none seem to capture the fault.  Looking at the Debug window, I can see an
>> IllegalStateException thrown by SQLiteCursor.finalize() line:603, then
>> NativeStart.run() line: not available [native method].
>>
>> This class encapsulates all my database activity.
>>
>>
>> /**
>>  * File:
>>  *     clsDB.java
>>  *
>>  * Notes:
>>  *     Contains the class clsDB.  This class manages the database SQLite
>> interface
>>  *
>>  * Methods:
>>  *    clsDB                - Constructor
>>  *    getImgResource        - Returns a Image resource from database
>>  *    getImgResources        - Returns an array containing all image
>> resource id's
>>  *    insertResourceId    - Insert resource identifier into database
>>  *    onCreate            - Called when database is created
>>  *    onUpdate            - Called when database is updated
>>
>>  
>> *******************************************************************************
>>  * History:
>>  * 12/06/2011 SP, Created class
>>  */
>> package uk.co.cantley.avasig;
>>
>> import android.content.ContentValues;
>> import android.content.Context;
>> import android.database.Cursor;
>> import android.database.sqlite.SQLiteDatabase;
>> import android.database.sqlite.SQLiteOpenHelper;
>> import android.util.Log;
>>
>> public class clsDB extends SQLiteOpenHelper {
>>     private static final String TAG = "clsDB";
>>
>>     private static final int    DB_VERSION = 1;
>>     private static final String DB_NAME = "avasig";
>>     private static final String TBL_IMG = "tblImgs";
>> // Image fields
>>     private static final String IMG_ID      = "imgID";
>>     private static final String IMG_RESID = "resID";
>> /**
>>  * Constructor
>>  * @param context, the application context
>>  */
>>     public clsDB(Context context) {
>>         super( context, DB_NAME, null, DB_VERSION );
>>     }
>> /**
>>  * Method:
>>  *     getImgResource
>>  *
>>  * Parameters:
>>  *     intResId, the image resource identifier
>>  *
>>  * Returns:
>>  *     The record identifier
>>  */
>>     public int getImgResource( int intResId ) {
>>         try{
>>             SQLiteDatabase db = getWritableDatabase();
>>             Cursor cursor = db.rawQuery(
>>                     "SELECT " +
>>                         "* " +
>>                     " FROM " +
>>                         TBL_IMG +
>>                     " WHERE " +
>>                         IMG_RESID + "='" + intResId + "'",
>>                     new String[] {} );
>>             if ( cursor != null ) {
>>                 if ( cursor.moveToFirst() == true ) {
>>                     int intColIdx = cursor.getColumnIndex( IMG_RESID );
>>                     return cursor.getInt( intColIdx );
>>                 }
>>             }
>>         } catch( Exception ex ) {
>>             Log.e(TAG, ex.getMessage() );
>>         }
>>         return -1;
>>     }
>> /**
>>  * Method:
>>  *    getImgResources
>>  *
>>  * Parameters:
>>  *     none
>>  *
>>  * Returns:
>>  *     An array of image resource identifiers
>>  */
>>     public int[] getImgResources() {
>>         try{
>>             SQLiteDatabase db = getWritableDatabase();
>>             Cursor cursor = db.rawQuery(
>>                     "SELECT " +
>>                         "* " +
>>                     " FROM " +
>>                         TBL_IMG,
>>                     new String[] {} );
>>             if ( cursor != null ) {
>>                 int intCount = cursor.getCount();
>>
>>                 if ( intCount > 0 ) {
>>                     int[] aryResIds = new int[intCount];
>>                     if( cursor.moveToFirst() == true ) {
>>                         int intColIdx = cursor.getColumnIndex( IMG_RESID
>> );
>>                         int i = 0;
>>                         do{
>>                             aryResIds[i++] = cursor.getInt( intColIdx );
>>                         } while( cursor.moveToNext() == true );
>>                     }
>>                     return aryResIds;
>>                 }
>>             }
>>         } catch( Exception ex ) {
>>             Log.e(TAG, ex.getMessage() );
>>         }
>>         return null;
>>     }
>> /**
>>  * Method:
>>  *     insertResourceId
>>  *
>>  * Parameters:
>>  *     intResId, the image resource identifier
>>  *
>>  * Returns:
>>  *     The record identifier
>>  */
>>     public long insertResourceId( int intResId ) {
>>         try{
>>             int intTmp = getImgResource( intResId );
>>
>>             if ( intTmp > 0 ) {
>> // This record already exists, abort!
>>                 return intResId;
>>             }
>>             SQLiteDatabase db = getWritableDatabase();
>>             ContentValues cv = new ContentValues();
>>             cv.put(IMG_RESID, intResId);
>>             return db.insertOrThrow(TBL_IMG, IMG_RESID, cv);
>>         } catch( Exception ex ) {
>>             Log.e(TAG, ex.getMessage() );
>>         }
>>         return -1;
>>     }
>> /**
>>  * Method:
>>  *     onCreate
>>  *
>>  * Parameters:
>>  *     db, the database
>>  *
>>  * Returns:
>>  *     none
>>  */
>>     @Override
>>     public void onCreate(SQLiteDatabase db) {
>>         db.execSQL( "CREATE TABLE " + TBL_IMG + " ( " +
>>                       IMG_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
>>                       IMG_RESID + " INTEGER NOT NULL )" );
>>      }
>> /**
>>  * Method:
>>  *     onUpgrade
>>  *
>>  * Parameters:
>>  *     db, the database
>>  *  intOld, the old version
>>  *  intNew, the new version
>>  *
>>  * Returns:
>>  *     none
>>  */
>>     @Override
>>     public void onUpgrade(SQLiteDatabase db, int intOld, int intNew) {
>>         db.execSQL( "DROP TABLE IF EXISTS " + TBL_IMG );
>>         onCreate( db );
>>      }
>> }
>>
>>
>> --
>> Regards,
>> Sy
>>
>>  --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/android-developers?hl=en
>
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en




-- 
Regards,
Sy

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to