1

アプリにたくさんのカーソルがあり、それらを明確に管理しようとしています。tutosで説明されているように、最後に閉じます。しかし、ICSを搭載したNexus Sで、アプリを再開するとクラッシュしました

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;
}

ご覧のとおり、私は「startManagingCursor」を使おうとしましたが、それはさらに最悪です...誰かがこれに直面し、最終的にそれを解決しましたか?LoaderManagerを使用する必要がありますか?

4

5 に答える 5

1

アンドロイドのドキュメントによると

警告: このメソッドを使用して取得したカーソルで close() を呼び出さないでください。アクティビティが適切なタイミングで呼び出しを行うためです。ただし、マネージド クエリからカーソルで stopManagingCursor(Cursor) を呼び出した場合、システムはカーソルを自動的に閉じません。その場合は、close() を呼び出す必要があります。

cursor.close();コードで削除してみてください。

于 2012-12-23T11:16:32.450 に答える
0

ここに答えがありました:

manageQueryは、MANAGEDカーソル(StartManagingCursorで提供されるカーソル)を開くだけです。したがって、カーソルを閉じる必要はありません。

ただし、このアプリでは1つ以上のカーソルを使用するため、管理カーソルを使用するのはひどいので、「単純な」カーソルを使用して、終了後にカーソルを閉じます。したがって、ManagedQueryの代わりに単純なクエリを実行する必要がありました...

于 2012-02-02T13:29:47.543 に答える
0

コードを調べていますが、カーソルを開く/開始する/再作成する onResume の場所が見つかりません。ここでエラーを探します。

于 2012-01-23T21:26:24.717 に答える
0

アクティビティ メソッド startManagingCursor(...) を使用する 2 つのオプションがあります。または、onResume() でクローズまたは null の cusor で何かをしようとするのをやめるだけです。

于 2012-01-23T22:24:14.583 に答える