Question

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();
} 
}
}
}
}
}
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top