android-获取联系电话
作者:互联网
我在获取联系电话时遇到麻烦,我在日志猫说中一直出错
02-24 19:40:42.772: ERROR/CursorWindow(21467): Bad request for field slot 0,-1. numRows = 1, numColumns = 24
这是我的代码
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.os.Bundle;
public class Contacts extends Activity {
private ListView mContactList;
private Button mAddContact;
private boolean mShowInvisible = false;
boolean set;
public String name = "";
public String id;
public String phone;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_listview);
mContactList = (ListView) findViewById(R.id.contactList);
mAddContact = (Button) findViewById(R.id.addContactButton);
mAddContact.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i,1);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(resultCode == RESULT_OK){
getContactData(data);
}
}
public void getContactData(Intent data){
ContentResolver cr = getContentResolver();
Uri contactData = data.getData();
Log.v("Contact", contactData.toString());
Cursor c = managedQuery(contactData,null,null,null,null);
if(c.moveToFirst()){
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
Log.v("Contact", "ID: " + id.toString());
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.v("Contact", "Name: " + name.toString());
if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID +" = ?", new String[]{id}, null);
while(pCur.moveToNext()){
phone = c.getString(c.getColumnIndex(Phone.NUMBER));
Log.v("getting phone number", "Phone Number: " + phone);
}
}
}
}
一切正常,直到我尝试获取电话号码为止
解决方法:
不应该是:
while(pCur.moveToNext()){
phone = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
Log.v("getting phone number", "Phone Number: " + phone);
}
也就是说,使用pCur而不是“ c”光标来保存联系人?
标签:contacts,contactscontract,android 来源: https://codeday.me/bug/20191023/1913316.html