Android - 获取所有联系人列表时出错

时间:2015-04-10 10:01:51

标签: java android android-contacts android-logcat android-cursor

我正在尝试从设备中检索所有联系号码。我已经为此编写了代码段。但是我在检索联系号码列表时遇到错误。我在这里发布我的代码和错误日志。
感谢。

onCreate of MainActivity中的代码

        contactListText = (TextView) findViewById(R.id.ContactsListText);
        ContactsProvider c = new ContactsProvider(this);
        c.setContactsString();
        String contactList = c.getContactString();
        contactListText.setText(contactList);

ContactsProvider.java

public class ContactsProvider {

    ArrayList<String> contactList;
    Context context;
    String name;

    public ContactsProvider(Context context) {
        this.context = context;
    }

    public ArrayList<String> getContactList() {
        return contactList;
    }

    public void setContactsString() {

        contactList = new ArrayList<String>();

        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER};
         Cursor people = context.getContentResolver().query(uri, projection, null, null, null);

         int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

         people.moveToFirst();
         do {
             String number = people.getString(indexNumber);
             contactList.add(number);

         } while (people.moveToNext());
    }

    public String getContactString() {

        if (contactList.size() > 0) {
            StringBuilder contactString = new StringBuilder();

            for (String n : contactList) {

                if (n.length() >= 10  && !n.startsWith("0217")) {
                    n = n.replace(" ", "");
                    n = n.replace("+", "");
                    n = n.replace("-", "");
                    n = n.substring(n.length() - 10);

                    if (contactString.length() != 0) {
                        contactString.append(",");
                    }
                    contactString.append(n);
                }

            }

            return contactString.toString();
        } else {
            return null;
        }

    }
}

logcat的

04-10 09:17:32.303: E/AndroidRuntime(1098): FATAL EXCEPTION: main
04-10 09:17:32.303: E/AndroidRuntime(1098): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beproject.ourway/com.beproject.ourway.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.os.Looper.loop(Looper.java:137)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at java.lang.reflect.Method.invoke(Method.java:511)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at dalvik.system.NativeStart.main(Native Method)
04-10 09:17:32.303: E/AndroidRuntime(1098): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.database.CursorWrapper.getString(CursorWrapper.java:114)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at com.beproject.ourway.system.ContactsProvider.setContactsString(ContactsProvider.java:37)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at com.beproject.ourway.MainActivity.onCreate(MainActivity.java:25)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.app.Activity.performCreate(Activity.java:5104)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-10 09:17:32.303: E/AndroidRuntime(1098):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-10 09:17:32.303: E/AndroidRuntime(1098):     ... 11 more

2 个答案:

答案 0 :(得分:1)

try {
Log.d(TAG, "Contacts: " + people.getCount());
        people.moveToFirst();
        while (!people.isAfterLast()) {
            String number = people.getString(indexNumber);
         people.add(number);
        }
    } catch (Exception ignored) {
    }

答案 1 :(得分:1)

代码:

int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
    String number = people.getString(indexNumber);
    contactList.add(number);
} while (people.moveToNext());

不对。你为什么打电话给String number = people.getString(indexNumber);?它毫无意义。

试试这个:

while (people.moveToNext()){
    String number = people.getString(people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    contactList.add(number);
}