gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:23:05 25 4
gpt4 key购买 nike

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

最佳答案

根据安卓文档

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

所以请尝试在您的代码中删除 cursor.close();

关于android - 恢复我的应用程序使其因光标管理而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8978479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com