A common mistake is the improper way to use cursors. It is very common to use a cursor and forget to close it. This produce an IllegalStateException.

A code similar to the following code example will produce an error the moment you leave the activity:

private void readContactData(long contactDataId) {
   Log.d(TAG, 'readContactData()');
   dbContact.open();
   Cursor cursor = dbContact.getContact(contactDataId);
   if (cursor.moveToFirst()) {
     ...
   } else {
     ...
   }
   dbContact.close();
   ...
}

The problem is that you are not closing the cursor properly.
You could try the following variable of the previous code:

private void readContactData(long contactDataId) {
   Log.d(TAG, 'readContactData()');
   dbContact.open();
   Cursor cursor = dbContact.getContact(contactDataId);
   if (cursor.moveToFirst()) {
     ...
   } else {
     ...
   }
   cursor.close();
   dbContact.close();
   ...
}

However, there are cases that this code produce a warning when cursor calls the method finalizer(). This seems to be a problem with Droid 2.

This method seems to work better:

private void readContactData(long contactDataId) {
    Log.d(TAG, 'readContactData()');
    Cursor cursor = null;
    try{
        dbContact.open();
        cursor = dbContact.getContact(contactDataId);
        if (cursor.moveToFirst()) {
            ...
        } else {
            ...
        }
        dbContact.close();
    }finally{
        if (cursor != null){
            cursor.deactivate();
            cursor.close();
        }
    }
}

Finally, there is always the option of using startManagingCursor(); however, this will be deprecated and replaced with CursorLoader class when using API 11.

© 2011, Alejandro G. Carlstein Ramos Mejia. All rights reserved.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

*

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette