gpt4 book ai didi

android - 获取运行时异常列 data2 无效?

转载 作者:行者123 更新时间:2023-11-29 14:04:10 26 4
gpt4 key购买 nike

我正在尝试获取联系人姓名及其类型,但在代码中标记的行中出现此异常。我在添加类型之前获取了名称,但现在出现此异常。请帮助。提前谢谢。

 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();
}
}
}
}
}
}

最佳答案

在投影中,Phone.Type 不是您正在查询的联系人表的一部分。详细说明您正在尝试什么。

如果您正在查看电话类型,那么您应该查询数据表

注意:我假设您使用的是 Android 2.0 + 而不是 1.5 或 1.6

关于android - 获取运行时异常列 data2 无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8061834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com