大家好,我在准备联系人列表时遇到以下异常。
04-13 13:51:15.210: E/AndroidRuntime(7343): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
这是我的getContact函数
public static ContactList getContactList(Context context){
ContactList contactList = new ContactList(RequestStatus.CONTACT_LIST);
Cursor people = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
while(people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
String number = people.getString(numberFieldColumnIndex);
contactList.addContact(new Contact(contact,number));
}
people.close();
return contactList;
}
以下行抛出异常。
String number = people.getString(numberFieldColumnIndex);
可能出错?
答案 0 :(得分:1)
您需要先移至第一列,以确保光标位于有效索引上:
if (people.moveToFirst()) {
do {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
String number = people.getString(numberFieldColumnIndex);
contactList.addContact(new Contact(contact,number));
}while(people.moveToNext());
}
答案 1 :(得分:1)
您可以使用以下方法获取联系人列表::
private ContactList getDetails(){
ContactList contactList = new ContactList(RequestStatus.CONTACT_LIST);
Uri uri = contactsContract.CommonDataKinds.Phone.CONTENT_URI;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor names = getContentResolver().query(uri, projection, null, null, null);
int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
names.moveToFirst();
do {
String name = names.getString(indexName);
Log.e("Name new:", name);
String number = names.getString(indexNumber);
Log.e("Number new:","::"+number);
contactList.addContact(new Contact(name,number));
} while (names.moveToNext());
return contactList;
}
答案 2 :(得分:0)
我可以告诉你有关异常的事。这只是我没有返回你需要的行。请求无效列
答案 3 :(得分:0)
使用
public static void getContactNumbers(Context context) {
String contactNumber = null;
int contactNumberType = Phone.TYPE_MOBILE;
String nameOfContact = null;
if (ApplicationConstants.phoneContacts.size() <= 0) {
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(BaseColumns._ID));
nameOfContact = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id },
null);
while (phones.moveToNext()) {
contactNumber = phones.getString(phones
.getColumnIndex(Phone.NUMBER));
contactNumberType = phones.getInt(phones
.getColumnIndex(Phone.TYPE));
Log.i(TAG, "...Contact Name ...." + nameOfContact
+ "...contact Number..." + contactNumber);
}
phones.close();
}
}
}// end of contact name cursor
cur.close();
}
}
当我遇到同样的问题时,这对我有什么帮助