0

我正在制作一个小型 Android 应用程序,它允许用户从您的联系人列表中选择一个电话号码。当用户选择同一个联系人时,我的应用程序崩溃。

这里是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)

这是代码的一部分:

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();

你能帮我吗?谢谢

4

1 回答 1

0

尝试将光标关闭调用移动到 destroy() 方法中并远离它现在所在的位置。

于 2014-05-02T20:05:48.553 回答