读取联系人时Cursor.moveToNext()中的异常

时间:2011-03-07 05:52:08

标签: android exception contacts

我正在使用以下代码来读取联系人并将一些信息存储在RAM中。在某些手机上,这不起作用。抛出此代码的异常。我认为它来自moveToNext()。有人可以看看它并告诉我他是否看错了。代码下方是开发人员控制台中错误报告的堆栈跟踪。

String[] ContactsProjection = new String[] {ContactsContract.Contacts._ID,
                                        ContactsContract.Contacts.DISPLAY_NAME};
String[] PhoneProjection    = new String[] {Phone.NUMBER, Phone.TYPE, Phone.IS_PRIMARY};

/* Make a query for all contacts that have at least one phone number */
Cursor PhoneContacts = theActivity.managedQuery(ContactsContract.Contacts.CONTENT_URI,                    
                     ContactsProjection,
                     ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'",
                     null, null);

if(PhoneContacts == null)
return false;

String Id, Name;

/* Iterate through contacts and store them in RAM */
while(PhoneContacts.moveToNext()
{
/* ID and name can already be stored. Because there has to be least one phone number */ 
Id   = PhoneContacts.getString( 
                      PhoneContacts.getColumnIndex(ContactsContract.Contacts._ID));
Name = PhoneContacts.getString( 
                 PhoneContacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

if((Id != null) && (Name != null))
{
/* Now get all available phone numbers for the current contact*/
Cursor Numbers = theActivity.managedQuery(Phone.CONTENT_URI, PhoneProjection, 
                                          Phone.CONTACT_ID + " = " + Id, null, null);

if(Numbers != null)
{
  while(Numbers.moveToNext()) /* Store all phone numbers */
  {
    String Number = Numbers.getString(Numbers.getColumnIndex(Phone.NUMBER));
    int    Type   = Numbers.getInt(Numbers.getColumnIndex(Phone.TYPE));
mCallOptions.add(new CallOption(Id, Name, Number, Type));
      }
    }
   }
 }

获取错误如下:

java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, display_name FROM view_htc_contacts_restricted WHERE ((has_phone_number = '1') AND (( ext_account_Type<>'DeviceOnly' ) OR ( ext_account_Type IS NULL ) )))
at android.os.Parcel.readException(Parcel.java:1255)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
at android.database.BulkCursorProxy.getWindow(BulkCursorNative.java:217)
at android.database.BulkCursorToCursorAdaptor.onMove(BulkCursorToCursorAdaptor.java:109)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:188)
at android.database.AbstractCursor.moveToNext(AbstractCursor.java:256)
at android.database.CursorWrapper.moveToNext(CursorWrapper.java:196)
at CallCommandHandler.boInit(CallCommandHandler.java:84)
at VoiceProcessor$1.run(VoiceProcessor.java:86)
at java.lang.Thread.run(Thread.java:1102)

2 个答案:

答案 0 :(得分:1)

使用如下的循环..

if(PhoneContacts.moveToFirst()){
        do{
            //YOUR CODE HERE                 
        }while(PhoneContacts.moveToNext());
    }

告诉我它是否有效..

答案 1 :(得分:0)

添加此PhoneContacts.movetofirst();在循环之前..并检查它会发生什么..