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

发布于 2024-12-14 10:29:04 字数 2386 浏览 0 评论 0原文

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

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

I am trying to get the contacts name and their types but getting this exception at line marked in the code.I was getting the names prior to adding the types,but now getting this exception.please help.thanks in advance.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南烟雨〆相思醉 2024-12-21 10:29:04

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

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

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

In projection, Phone.Type is not a part of Contacts Table which you are querying.
Explain in detail, what you are trying.

If you are looking at Type of Phone, then you should be querying Data table

NOTE: I presume that you are using Android version 2.0 + and not 1.5 or 1.6

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文