我在收到联系电话时遇到问题,我在日志猫中不断收到错误
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);
}
}
}
}
一切都很好,直到我尝试获取电话号码
答案 0 :(得分:5)
不应该是:
while(pCur.moveToNext()){
phone = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
Log.v("getting phone number", "Phone Number: " + phone);
}
也就是说,pCur而不是'c'光标,它保存了联系人?
答案 1 :(得分:0)
您需要在调用“moveToNext()”之前重置光标。只需调用“pCur.moveToFirst();”就在此之前。这应该会有所帮助。
答案 2 :(得分:0)
我明白了,我拿出了while循环,现在我的数字很好