我正在尝试检索存储在手机中的联系人的姓名和类型,但出现此异常。我之前得到了名字但得到了例外
android.database.CursorIndexOutOfBoundsException:索引 -1 请求大小为 10
当试图一起检索名称和类型时。请帮忙。提前致谢。
package application.test;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] projection = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
String[] projection1=new String[]{Phone.TYPE};
String[] projection2=new String[]{ContactsContract.Contacts._ID};
ContentResolver cr = getContentResolver();
ContentResolver ncr=getContentResolver();
ContentResolver icr=getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");
Cursor ncur=ncr.query(ContactsContract.Data.CONTENT_URI, projection1, null, null,null);
Cursor icur = icr.query(ContactsContract.RawContacts.CONTENT_URI, projection2,null, null, Contacts._ID + " ASC");
if (cur.getCount() >0 && ncur.getCount()>0 && icur.getCount()>0)
{
while (cur.moveToNext()&& ncur.moveToNext()&& icur.getCount()>0)
{
String id = icur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String type=ncur.getString(ncur.getColumnIndex(Phone.TYPE));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
Cursor typecur = ncr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null)
while (pCur.moveToNext()&& typecur.moveToNext())
{
Log.d("names",name);
Log.d("types",type);
pCur.close();
}
}
}
}
}
}