title: Retrieving phone contacts on Android 3.0
body: <p>Hi I use the following code to retrieve the phone contacts of the device. </p>
  
  <pre><code>import android.app.Activity;
  import android.content.ContentResolver;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.provider.Contacts;
  import android.provider.Contacts.People;
  
  public class TestContacts extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          ContentResolver cr = getContentResolver();
          Cursor cur = cr.query(People.CONTENT_URI, 
              null, null, null, null);
          if (cur.getCount() &gt; 0) {
           while (cur.moveToNext()) {
               String id = cur.getString(cur.getColumnIndex(People._ID));
               String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
           }
  
          }
          if (Integer.parseInt(cur.getString(
                  cur.getColumnIndex(People.PRIMARY_PHONE_ID))) &gt; 0) ---Error at this line---
   {
              Cursor pCur = cr.query(
                      Contacts.Phones.CONTENT_URI, 
                      null, 
                      Contacts.Phones.PERSON_ID +" = ?", 
                      new String[]{id}, null);
              int i=0;
              int pCount = pCur.getCount();
              String[] phoneNum = new String[pCount];
              String[] phoneType = new String[pCount];
              while (pCur.moveToNext()) {
                  phoneNum[i] = pCur.getString(
                                     pCur.getColumnIndex(Contacts.Phones.NUMBER));
                  phoneType[i] = pCur.getString(
                                     pCur.getColumnIndex(Contacts.Phones.TYPE));
                  i++;
              } 
          }
      }
  
  }
  </code></pre>
  
  <p>But I get CursorIndexOutOfBound Exception at the bold line. The exception as follows</p>
  
  <pre><code>05-13 14:27:26.910: ERROR/AndroidRuntime(2432): Uncaught handler: thread main exiting due to uncaught exception
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432): **android.database.CursorIndexOutOfBoundsException: Index 13 requested, with a size of 13**
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.database.CursorWrapper.getString(CursorWrapper.java:135)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.fsp.mus.UTell_Sms$2.onClick(UTell_Sms.java:101)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.View.performClick(View.java:2347)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.View.onTouchEvent(View.java:4185)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.View.dispatchTouchEvent(View.java:3717)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1659)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.os.Handler.dispatchMessage(Handler.java:99)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.os.Looper.loop(Looper.java:123)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at android.app.ActivityThread.main(ActivityThread.java:4203)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at java.lang.reflect.Method.invoke(Method.java:521)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
  05-13 14:27:26.910: ERROR/AndroidRuntime(2432):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>