Android 恢复我的应用程序会使其因光标管理而崩溃

Android 恢复我的应用程序会使其因光标管理而崩溃,android,cursor,resume,Android,Cursor,Resume,我的应用程序中有很多光标,我正试图清晰地管理它们。 我按照图托斯中的解释做了:在最后关闭它们。 但是,在我的Nexus S和ICS上,当我恢复我的应用程序时,我崩溃了 01-23 21:52:32.125: E/AndroidRuntime(14037): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed. 我在网上看到一些答案,说我应该使用

我的应用程序中有很多光标,我正试图清晰地管理它们。 我按照图托斯中的解释做了:在最后关闭它们。 但是,在我的Nexus S和ICS上,当我恢复我的应用程序时,我崩溃了

01-23 21:52:32.125: E/AndroidRuntime(14037): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
我在网上看到一些答案,说我应该使用LoaderManager来管理它们,但我不确定它是否适合我的情况。 这是我使用游标的函数:

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;
}
publicstatichashmap getContacts(BaseActivity){
HashMap contactMap=新建HashMap();
ArrayList allPhones=null;
试一试{
Log.d(Constants.LOGTAG,“=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!”;
//运行查询
把电话串起来;
int-id;
Uri=Contacts contract.Contacts.CONTENT\u Uri;
字符串[]投影=新字符串[]{
联系人合同联系人。\u ID,
Contacts contract.Contacts.DISPLAY\u NAME,
Contacts contract.Contacts.HAS_PHONE_NUMBER};
Cursor=activity.managedQuery(uri、投影、null、null、null);
//activity.startManagingCursor(游标);
if(cursor.moveToFirst()){
而(!cursor.isAfterLast()){
id=cursor.getInt(0);
if(cursor.getInt(2)==1){
联系人c=新联系人(id,cursor.getString(1),null,getAllPhoneNumber(活动,id));
Contact c2=新联系人(id,cursor.getString(1),null,getAllPhoneNumber(活动,id));
allPhones=c.getPhoneNumber();
用于(字符串电话:所有电话){
电话=电话;
c2.getPhoneNumber().clear();
c2.设定指标(“+33”);
thePhone=thePhone.replaceAll(“,”);
如果(电话以(“00”)开头){//On vire le 0033
电话=电话子串(4);
}
else如果(phone.startsWith(“+”){//On vire le+33
电话=电话子串(3);
}
如果(电话以“0”开头){
电话=电话子串(1);
}
//c、 getPhoneNumber()。添加(电话);
c2.getPhoneNumber().add(电话);
contactMap.put(电话,c2);
}
}
cursor.moveToNext();
}
}
cursor.close();
}捕获(例外e){
e、 printStackTrace();
}
Whosupp.SetAddressBookHM(联系人地图);
返回联系人地图;
}
/**
*从通讯录中获取按名称排序的当前选定帐户的联系人列表
* 
*@返回所有联系人的列表。
*/
公共静态数组列表GetContactsOrderByName(BaseActivity活动){
ArrayList contactList=新建ArrayList();
ArrayList allPhones=null;
试一试{
Log.d(Constants.LOGTAG,“=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!=!”;
//运行查询
把电话串起来;
Uri=Contacts contract.Contacts.CONTENT\u Uri;
字符串[]投影=新字符串[]{
联系人合同联系人。\u ID,
Contacts contract.Contacts.DISPLAY\u NAME,
Contacts contract.Contacts.HAS_PHONE_NUMBER};
字符串排序器=Contacts contract.Contacts.DISPLAY\u NAME
+“整理本地化ASC”;
Cursor=activity.managedQuery(uri、投影、null、null、排序器);
//activity.startManagingCursor(游标);
if(cursor.moveToFirst()){
而(!cursor.isAfterLast()){
int id=cursor.getInt(0);
if(cursor.getInt(2)==1){
联系人c=新联系人(id,cursor.getString(1),null,getAllPhoneNumber(活动,id));
allPhones=c.getPhoneNumber();
用于(字符串电话:所有电话){
电话=电话;
c、 getPhoneNumber().clear();
c、 setIndicatif(“+33”);
thePhone=thePhone.replaceAll(“,”);
如果(电话以(“00”)开头){//On vire le 0033
电话=电话子串(4);
}
else如果(phone.startsWith(“+”){//On vire le+33
电话=电话子串(3);
}
如果(电话以“0”开头){
电话=电话子串(1);
}
//c、 getPhoneNumber()。添加(电话);
联系人列表。添加(c);
}
}
cursor.moveToNext();
}
}
cursor.close();
}捕获(例外e){
e、 printStackTrace();
}
返回联系人列表;
}
/**
*获取联系人的第一个电话号码
* 
*@param contactId
*联系人的身份证
*@返回联系人的第一个电话号码!
*/
公共静态ArrayList getFirstPhoneNumber(BaseActivity活动,int contactId){
最终字符串[]投影=新字符串[]{Phone.NUMBER};
ArrayList编号=新的ArrayList();
游标phone=activity.managedQuery(phone.CONTENT\u URI,投影,
Data.CONTACT_ID+“=?”,
新字符串[]{String.valueOf(contactId)},null);
//活动。开始管理光标(电话);
if(phone.moveToFirst()){
add(phone.getString(phone.getColumnIndex(phone.number));
}
phone.close();
返回号码;
}
/**
*获取联系人的所有电话号码
* 
*@param contactId
*联系人的身份证
*@返回联系人的所有电话号码列表
*/
公共静态数组列表getAllPhoneNumber(BaseActivity活动,int联系人
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (!dbHelper.db.isOpen()) {
            dbHelper.open();
        }

        cursor.requery();
    }