【发布时间】:2012-03-15 10:28:11
【问题描述】:
我想显示联系人的电话号码。如果我在模拟器中运行我的代码没有任何反应,但是当我在我的智能手机中运行它时,我在 logcat 中发现了这个错误---->
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
我正在使用的代码是
Cursor peopleCursor =
getContentResolver().query(ContactsContract.Contacts.CONTENT_URI
,null, null,null, null);
String [] nb = new String[peopleCursor.getCount()];
String [] Tname = new String[peopleCursor.getCount()];
if(peopleCursor.getCount()>0){
peopleCursor.moveToFirst();
Cursor numberCursor;
for(int i=0;i<peopleCursor.getCount();i++)
{
//get number
numberCursor=
getContentResolver().query(ContactsContract.
CommonDataKinds.Phone.CONTENT_URI, new String[]
{ContactsContract.CommonDataKinds.Phone.NUMBER}
,ContactsContract.CommonDataKinds.Phone._ID+"="+peopleCursor
.getString(peopleCursor.getColumnIndex(ContactsContract.Contacts._ID))
, null,null);
numberCursor.moveToFirst();
String number=numberCursor.getString(numberCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
nb[i]=number;
//get name
String name=peopleCursor.getString(numberCursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Tname[i]=name;
peopleCursor.moveToNext();
}
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.contact_entry, peopleCursor,nb
, new int[] {R.id.checkBox});
lv.setAdapter(adapter);
}
}
请问有什么解决办法吗?
【问题讨论】:
-
您的查询没有任何结果,因此它返回一个空光标给您。您的异常发生是因为您试图从不存在的光标中提取信息。我对 Contacts API 不够熟悉,无法确定为什么要返回空光标。
标签: android listview checkbox contactscontract