【问题标题】:getting run time exception invalid column data2?获取运行时异常无效列data2?
【发布时间】:2011-11-09 07:52:22
【问题描述】:

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

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

【问题讨论】:

  • 如果我的回答对您有所帮助。请接受它作为答案。

标签: android contacts runtimeexception illegalargumentexception


【解决方案1】:

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

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

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

【讨论】:

  • 我正在尝试检索存储在联系人中的姓名及其各自的类型,即(家庭、工作、手机)..
  • 联系人表中提供姓名,类型信息仅在数据表中可用。您需要查询 3 个不同的表以获取信息。 1.联系人表、原始联系人表和数据表
  • 为什么是原始联系人表..你能解释一下这件事吗?
  • 数据表存储各种带有原始联系人ID的信息。原始联系人表存储,联系人 ID
  • 我尝试查询不同的表,但同一行出现相同的错误....查看编辑后的代码.......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
  • 2012-04-28
  • 2012-08-07
  • 1970-01-01
  • 2019-02-22
  • 1970-01-01
  • 2018-08-20
相关资源
最近更新 更多