由于光标管理,恢复我的应用程序使其崩溃

发布于 2024-12-28 19:07:21 字数 7059 浏览 0 评论 0原文

我的应用程序中有很多光标,我试图清楚地管理它们。 我按照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 吗?

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

01-23 21:52:32.125: E/AndroidRuntime(14037): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.

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 :

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

享受孤独 2025-01-04 19:07:22

我正在研究代码,但无法找到打开/开始管理/重新创建光标的 onResume 的位置。我会在这里寻找错误。

I am looking into code and can't find where is onResume with opening/startmanaging/recreating the cursores. I would look for the error here.

只想待在家 2025-01-04 19:07:22

您有两种选择:使用活动方法 startManagingCursor(...) 或停止尝试对 onResume() 中已关闭或为 null 的 cusor 执行某些操作。

You have two options use the activity method startManagingCursor(...) or you just stop trying to do something with a cusor that is closed or null in the onResume().

ヤ经典坏疍 2025-01-04 19:07:22

答案是:

manageQuery 只需打开一个 MANAGED 游标(StartManagingCursor 提供的游标)。因此不需要关闭游标。

但是,由于在我们的应用程序中我们使用超过 1 个游标,因此使用托管游标会很糟糕,因此我们使用“简单”游标并在完成后关闭它们。所以我们只需要做一个简单的查询而不是 ManagedQuery...

Here was the answer :

manageQuery just open up a MANAGED cursor (one such provided with StartManagingCursor). Thus cursor do not need to be closed.

But, since in our app we use more then 1 cursor, it'd be awful to use managed cursor, so we use "simple" cursors and close them after we are done. So we just had to do a simple Query instead of ManagedQuery...

﹎☆浅夏丿初晴 2025-01-04 19:07:22

像这样编写onResume()

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (!dbHelper.db.isOpen()) {
            dbHelper.open();
        }

        cursor.requery();
    }

Write onResume() like this:

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (!dbHelper.db.isOpen()) {
            dbHelper.open();
        }

        cursor.requery();
    }
堇色安年 2025-01-04 19:07:21

根据android文档

警告:不要对使用此方法获得的游标调用 close(),
因为该活动会在适当的时间为您做到这一点。
但是,如果您在游标上调用 stopManagingCursor(Cursor)
托管查询,系统不会自动关闭游标,并且,
在这种情况下,您必须调用 close()。

因此,请尝试删除代码中的 cursor.close();

according to android documentation

Warning: Do not call close() on a cursor obtained using this method,
because the activity will do that for you at the appropriate time.
However, if you call stopManagingCursor(Cursor) on a cursor from a
managed query, the system will not automatically close the cursor and,
in that case, you must call close().

so please try removing cursor.close(); in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文