如何显示Android联系人列表

时间:2014-07-02 11:09:25

标签: android

当我点击显示按钮然后是错误。 但我已经给了许可联系人。

我无法理解的问题。

错误日志:

07-02 10:29:06.262: E/AndroidRuntime(3135): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
07-02 10:29:06.262: E/AndroidRuntime(3135):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
07-02 10:29:06.262: E/AndroidRuntime(3135):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
07-02 10:29:06.262: E/AndroidRuntime(3135):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
07-02 10:29:06.262: E/AndroidRuntime(3135):     at android.database.CursorWrapper.getString(CursorWrapper.java:114)
07-02 10:29:06.262: E/AndroidRuntime(3135):     at com.example.contentprovidernew_ahsanul.MainActivity.displayContracts(MainActivity.java:74)

我的Full code

2 个答案:

答案 0 :(得分:0)

在第86行,我认为这是问题所在。

变化:

                    phCursor.moveToFirst();
                     do{
                         String phoneNumber=phCursor.getString(phCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                         Toast.makeText(getApplicationContext(), phoneNumber+" "+ name, Toast.LENGTH_LONG).show();

                     }while(phCursor.moveToFirst());

为:

                    phCursor.moveToFirst();
                     do{
                         String phoneNumber=phCursor.getString(phCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                         Toast.makeText(getApplicationContext(), phoneNumber+" "+ name, Toast.LENGTH_LONG).show();
                     }while(phCursor.moveToNext());

答案 1 :(得分:0)

试试这个......

if(cursor!=null && cursor.getCount()>0){
cursor.moveToFirst(); // add this : moves to first 
            do{
                String id=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                 int hasphoneNumber=Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));

                 if(hasphoneNumber>0){
                     Cursor phCursor=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[]{id}, null);
                     if(phCursor!=null && phCursor.getCount()>0){
                        phCursor.moveToFirst();
                         do{
                             String phoneNumber=phCursor.getString(phCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                             Toast.makeText(getApplicationContext(), phoneNumber+" "+ name, Toast.LENGTH_LONG).show();
                         }while(phCursor.moveToNext());// change here : use moveToNext()
                     }
                     phCursor.close();
                 }

            }while(cursor.moveToNext());
        }