Note that there are some explanatory texts on larger screens.

plurals
  1. POResuming my app make it crash due to cursor management
    primarykey
    data
    text
    <p>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 </p> <pre><code>01-23 21:52:32.125: E/AndroidRuntime(14037): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed. </code></pre> <p>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 : </p> <pre><code>public static HashMap&lt;String, Contact&gt; getContacts(BaseActivity activity) { HashMap&lt;String, Contact&gt; contactMap = new HashMap&lt;String, Contact&gt;(); ArrayList&lt;String&gt; 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&lt;Contact&gt; getContactsSortedByName(BaseActivity activity) { ArrayList&lt;Contact&gt; contactList = new ArrayList&lt;Contact&gt;(); ArrayList&lt;String&gt; 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&lt;String&gt; getFirstPhoneNumber(BaseActivity activity, int contactId) { final String[] projection = new String[] { Phone.NUMBER }; ArrayList&lt;String&gt; number=new ArrayList&lt;String&gt;(); 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&lt;String&gt; 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&lt;String&gt; phoneNumber = new ArrayList&lt;String&gt;(); 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; } </code></pre> <p>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 ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload