我正在尝试获取联系人姓名及其类型,但是在代码中标记的行处获取此异常。我在添加类型之前获取了名称,但现在得到此异常。请事先帮助。谢谢。 / p>
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._ID,
ContactsContract.Contacts.DISPLAY_NAME,Phone.TYPE};
ContentResolver cr = getContentResolver();
ContentResolver ncr=getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");
Cursor ncur=ncr.query(ContactsContract.Data.CONTENT_URI, projection, null, null,Contacts.DISPLAY_NAME + " ASC");
Cursor icur = cr.query(ContactsContract.RawContacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");
if (cur.getCount() >0 && ncur.getCount()>0)
{
while (cur.moveToNext()&& ncur.moveToNext())
{
String id = icur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String type=ncur.getString(cur.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();
}
}
}
}
}
}
答案 0 :(得分:0)
在投影中,Phone.Type不是您要查询的Contacts Table的一部分。 详细解释,你在尝试什么。
如果您正在查看手机类型,那么您应该查询数据表
注意:我认为您使用的是Android版本2.0 +而不是1.5或1.6