Error staledataexception

466 views Asked by At

I'm making a small Android app, which allows the user to select from a your contact list, a phone numbers. My application crashes when the user selects the same contact.

Here LogCat:

E/AndroidRuntime(6745): FATAL EXCEPTION: main
E/AndroidRuntime(6745): java.lang.RuntimeException: Unable to resume activity{it.bisemanuDEV.callresponder/it.bisemanuDEV.callresponder.CallResponderListNumberActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2608)

this is the part of code:

Uri contactData = data.getData();

if (contactData != null) {
  Cursor c = managedQuery(contactData, null, null, null, null);
  if (c.moveToFirst()) {      
    try { 
      m_phones.add( c.getString( c.getColumnIndexOrThrow( ContactsContract.CommonDataKinds.Phone.NUMBER)).trim()); 
      m_contacts.add( c.getString( c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)).trim());

      // add new item to list     
      String tmp_phones = m_phones.toString();
      tmp_phones = tmp_phones.substring(1,tmp_phones.length() - 1);       

      // save to sharedpreferences    
      SharedPreferences.Editor editor = settings.edit();      
      editor.putString(BLACKLIST_PREF, tmp_phones);       
      editor.commit();    

      // refresh ui      
      m_adapter.notifyDataSetChanged();       // inform user      
      String toast_text = getString(R.string.phone_added);   
      Toast.makeText( getApplicationContext(), toast_text,Toast.LENGTH_SHORT ).show();

  } catch (Exception e) {     
      Log.e(TAG, e.getMessage());     
  }
}
c.close();

Can you help me please? Thanks

1

There are 1 answers

0
Cory Roy On

Try moving the cursor close call into the destroy() method and away from where it is now.