在Android 3.0上检索手机通讯录

时间:2011-05-13 09:02:22

标签: android

您好我使用以下代码检索设备的手机通讯录。

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

}

但是我在粗线处得到了CursorIndexOutOfBound异常。例外情况如下

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)

1 个答案:

答案 0 :(得分:4)

您正在引用已弃用的类Contacts,Phone

运行下面的代码,你的问题就会出现......

package com.test;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;

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(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (("1")
                        .equals(cur
                                .getString(cur
                                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_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(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        phoneType[i] = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                        i++;
                    }
                }

            }

        }

    }
}