1

I've got a lot of Cursor in my app and I'm trying to manage them clearly. I did as it's explained in tutos : closing them at the end. But, on my Nexus S with ICS, when I'm resuming my app, I got crash

01-23 21:52:32.125: E/AndroidRuntime(14037): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.

I saw some answers on the internet saying that I should use LoaderManager to manage them but I'm not sure it's adapted to my case. This is my functions using cursor :

public static HashMap<String, Contact> getContacts(BaseActivity activity) {
    HashMap<String, Contact> contactMap = new HashMap<String, Contact>();
    ArrayList<String> allPhones =null;
    try{
        Log.d(Constants.LOGTAG,"=!=!=!=!=!=!=!=!=!=! GET CONTACTS (HashMap) =!=!=!=!=!=!=!=!=!=!=!=!=!=!");
        // Run query
        String thePhone;
        int id;
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER };

        Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
        //activity.startManagingCursor(cursor);
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                id = cursor.getInt(0);
                if (cursor.getInt(2) == 1) {

                    Contact c = new Contact(id, cursor.getString(1),null,getAllPhoneNumber(activity, id));
                    Contact c2 = new Contact(id, cursor.getString(1),null,getAllPhoneNumber(activity, id));
                    allPhones = c.getPhoneNumber();
                    for(String phone:allPhones){
                        thePhone = phone;
                        c2.getPhoneNumber().clear();
                        c2.setIndicatif("+33");
                        thePhone = thePhone.replaceAll(" ", "");

                        if(thePhone.startsWith("00")){//On vire le 0033
                            thePhone = thePhone.substring(4);
                        }
                        else if(thePhone.startsWith("+")){//On vire le +33
                            thePhone =thePhone.substring(3);
                        }
                        if(thePhone.startsWith("0")){
                            thePhone = thePhone.substring(1);
                        }
                        //c.getPhoneNumber().add(thePhone);
                        c2.getPhoneNumber().add(thePhone);
                        contactMap.put(thePhone,c2);
                    }
                }
                cursor.moveToNext();
            }
        }
        cursor.close();
    }catch (Exception e) {
        e.printStackTrace();
    }
    Whosupp.setAdresseBookHM(contactMap);
    return contactMap;
}

/**
 * Obtains the contact list from the Adress Book for the currently selected account SORTED BY NAME
 * 
 * @return a list of all contact.
 */
public static ArrayList<Contact> getContactsSortedByName(BaseActivity activity) {
    ArrayList<Contact> contactList = new ArrayList<Contact>();
    ArrayList<String> allPhones =null;
    try{
        Log.d(Constants.LOGTAG,"=!=!=!=!=!=!=!=!=!=! GET CONTACTS (ArrayList) =!=!=!=!=!=!=!=!=!=!=!=!=!=!");
        // Run query
        String thePhone;
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER };

        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";

        Cursor cursor = activity.managedQuery(uri, projection, null, null, sortOrder);
        //activity.startManagingCursor(cursor);
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                int id = cursor.getInt(0);
                if (cursor.getInt(2) == 1) {


                    Contact c = new Contact(id, cursor.getString(1),null,getAllPhoneNumber(activity, id));

                    allPhones = c.getPhoneNumber();
                    for(String phone:allPhones){
                        thePhone = phone;
                        c.getPhoneNumber().clear();
                        c.setIndicatif("+33");
                        thePhone = thePhone.replaceAll(" ", "");

                        if(thePhone.startsWith("00")){//On vire le 0033
                            thePhone = thePhone.substring(4);
                        }
                        else if(thePhone.startsWith("+")){//On vire le +33
                            thePhone =thePhone.substring(3);
                        }
                        if(thePhone.startsWith("0")){
                            thePhone = thePhone.substring(1);
                        }
                        //c.getPhoneNumber().add(thePhone);

                        contactList.add(c);
                    }
                }
                cursor.moveToNext();
            }
        }
        cursor.close();
    }catch (Exception e) {
        e.printStackTrace();
    }

    return contactList;
}

/**
 * Get the first phone number of a contact
 * 
 * @param contactId
 *            the contact's id
 * @return the first phone number of the contact ! 
 */
public static ArrayList<String> getFirstPhoneNumber(BaseActivity activity, int contactId) {
    final String[] projection = new String[] { Phone.NUMBER };
    ArrayList<String>  number=new ArrayList<String>();
    Cursor phone = activity.managedQuery(Phone.CONTENT_URI, projection,
            Data.CONTACT_ID + "=?",
            new String[] { String.valueOf(contactId) }, null);
    //activity.startManagingCursor(phone);

    if (phone.moveToFirst()) {
        number.add(phone.getString(phone.getColumnIndex(Phone.NUMBER)));
    }
    phone.close();
    return number;
}

/**
 * Get all phone number of a contact
 * 
 * @param contactId
 *            the contact's id
 * @return a list of all phone number of the contact
 */
public static ArrayList<String> getAllPhoneNumber(BaseActivity activity, int contactId) {
    final String[] projection = new String[] { Phone.NUMBER };
    String number = "";
    Cursor phone = activity.managedQuery(Phone.CONTENT_URI, projection,
            Data.CONTACT_ID + "=?",
            new String[] { String.valueOf(contactId) }, null);
    //activity.startManagingCursor(phone);
    ArrayList<String> phoneNumber = new ArrayList<String>();

    if (phone.moveToFirst()) {
        final int contactNumberColumnIndex = phone.getColumnIndex(Phone.NUMBER);

        while (!phone.isAfterLast()) {
            number = phone.getString(contactNumberColumnIndex);
            if(number.contains("305875"))               Log.d(Constants.LOGTAG,"I'm here "+number);
            phoneNumber.add(number);
            phone.moveToNext();
        }
    }
    phone.close();
    return phoneNumber;
}

As you can see, I've tried to use "startManagingCursor" but it's even worst... Does anyone be confronted to this and finally solved it ? Should I use LoaderManager ?

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Arkezis
  • 416
  • 1
  • 5
  • 9
  • just do startManagingCursor(cursor) and remove the close() at end and try. – kosa Jan 23 '12 at 21:15
  • [Android android.database.StaleDataException][1] Check this question and answer. it may help you. [1]: http://stackoverflow.com/a/10603317 – Siddhesh Dec 23 '12 at 11:07

5 Answers5

1

according to android documentation

Warning: Do not call close() on a cursor obtained using this method, because the activity will do that for you at the appropriate time. However, if you call stopManagingCursor(Cursor) on a cursor from a managed query, the system will not automatically close the cursor and, in that case, you must call close().

so please try removing cursor.close(); in your code.

Siddhesh
  • 1,370
  • 11
  • 28
0

Write onResume() like this:

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (!dbHelper.db.isOpen()) {
            dbHelper.open();
        }

        cursor.requery();
    }
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
0

I am looking into code and can't find where is onResume with opening/startmanaging/recreating the cursores. I would look for the error here.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • In fact, my onResume is in an other Activity and call lot of methods where some of them call some methods from the given class ^^ – Arkezis Jan 24 '12 at 08:36
  • But you can put a new onResume here and check all cursors for being opened and/or open them if needed. When they will in one place you can control them and rechecking can find you an error or two in future. – Gangnus Jan 24 '12 at 09:16
  • It's just a class ContactDBAdapter ! I must create a static onResume() in it and call it from my onResume() in my Activity ? – Arkezis Jan 24 '12 at 10:16
  • Oh, pardon, I don't see it. You are writing: " when I'm resuming my app..." I mean that very onResume. And maybe it should be here, in code, too? You see, it is difficult to find where a cursor is closing or not opening if we do not see all places where such activity should be done. – Gangnus Jan 24 '12 at 10:41
  • I don't have all the code with me, I'll post it in 7 or 8 hours. But it's a really simple way, in my ContactStateActivity, in my onResume, I'm calling AsyncTask which call few methods in ContactDBAdapter. – Arkezis Jan 24 '12 at 11:00
0

You have two options use the activity method startManagingCursor(...) or you just stop trying to do something with a cusor that is closed or null in the onResume().

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • As you can see, I've already tried with startManagingCursor(...) but I get nullPointer on some Cursor... For exemple, in my onResume(...) I call methods which calls getFirstPhoneNumber(...) ! How the Cursor phone can be null in line phone.moveToFirst(), I created it just 2 lines up ! ! – Arkezis Jan 24 '12 at 08:29
0

Here was the answer :

manageQuery just open up a MANAGED cursor (one such provided with StartManagingCursor). Thus cursor do not need to be closed.

But, since in our app we use more then 1 cursor, it'd be awful to use managed cursor, so we use "simple" cursors and close them after we are done. So we just had to do a simple Query instead of ManagedQuery...

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Cehm
  • 1,502
  • 1
  • 12
  • 14