Skip to content Skip to sidebar Skip to footer

Phone Type And Email Type Not Working?

The phone type and Email type in my project has some problem.I am using 2.1 platform, there was no problem in my project before adding the Phone type and the Email type, after usin

Solution 1:

I got the error in your code. You are checking the phoneType as well as the emailtype after moving the cursor beyond the last record (pCur.moveToNext() after the while loop goes beyond the last record). You need to check it within the while loop and not after if. Change the following:

while (pCur.moveToNext()) 
        {
             phoneNumber.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 

             int contactPhoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

             switch(contactPhoneType){
                case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 
                     Ptype = "Home";
                     break;
           }                  
       }

do the same correction for the emailType as well.


Solution 2:

Might it be this line:

while (cursor.moveToNext()) 

To me, that looks like you are moving the cursor before you've even read the first cursor position, and you're getting an CursorIndexOOB exeption.

Try a do-while loop instead, see if that helps.

Also, check that your application has the permission to read contact data.

uses-permission android:name="android.permission.READ_CONTACTS"

Solution 3:

as looks problem is with index

android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1

gor getting the Phone.TYPE can try below code as per this http://www.vtgroup.com/#ContactsContract

    //  Get all phone numbers.
    //
    Cursor phones = cr.query(Phone.CONTENT_URI, null,
        Phone.CONTACT_ID + " = " + contactId, null, null);
    while (phones.moveToNext()) {
        String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
        switch (type) {
            case Phone.TYPE_HOME:
                // do something with the Home number here...
                break;
            case Phone.TYPE_MOBILE:
                // do something with the Mobile number here...
                break;
            case Phone.TYPE_WORK:
                // do something with the Work number here...
                break;
            }
    }
    phones.close();

Solution 4:

As told by Dheeresh you have to change that condition


Post a Comment for "Phone Type And Email Type Not Working?"