Android 应用程序读取联系人电话号码时出现问题

发布于 2024-10-26 13:34:10 字数 4327 浏览 1 评论 0原文

我试图读取联系人的电话号码,但无法让它工作...

我的代码与 此处


ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);

    if (cur.getCount() > 0) 
    {
        contacts = new String[cur.getCount()];
        numbers = new String[cur.getCount()];
        int index = 0;
        while (cur.moveToNext()) 
        {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            contacts[index] = name;             
            MyListAdapter listAdapt = new MyListAdapter(MeetUp.this, R.layout.row, contacts);
            MeetUp.this.setListAdapter(listAdapt);


            if ( Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
            {
                Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                while(pCur.moveToNext());
                {
                    numbers[index] = pCur.getString( pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));               
                }
            }
            index++;
        }
    }

我的 logcat 输出:

03-25 16:55:08.399: ERROR/AndroidRuntime(5383): java.lang.RuntimeException: Unable to start activity ComponentInfo{michaels.meeting/michaels.meeting.MeetUp}: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.os.Looper.loop(Looper.java:144)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread.main(ActivityThread.java:4937)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at java.lang.reflect.Method.invoke(Method.java:521)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at dalvik.system.NativeStart.main(Native Method)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): Caused by: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.database.CursorWrapper.getString(CursorWrapper.java:140)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at michaels.meeting.MeetUp.onCreate(MeetUp.java:47)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     ... 11 more

失败的联系人肯定有一个号码,但调试后我认为 pCur 没有数字列..有什么想法为什么会发生这种情况吗?提前致谢——迈克

Im trying to read the phone number of a contact but I cant get it to work...

My code is exactly the same as here


ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);

    if (cur.getCount() > 0) 
    {
        contacts = new String[cur.getCount()];
        numbers = new String[cur.getCount()];
        int index = 0;
        while (cur.moveToNext()) 
        {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            contacts[index] = name;             
            MyListAdapter listAdapt = new MyListAdapter(MeetUp.this, R.layout.row, contacts);
            MeetUp.this.setListAdapter(listAdapt);


            if ( Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
            {
                Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
                while(pCur.moveToNext());
                {
                    numbers[index] = pCur.getString( pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));               
                }
            }
            index++;
        }
    }

My logcat output:

03-25 16:55:08.399: ERROR/AndroidRuntime(5383): java.lang.RuntimeException: Unable to start activity ComponentInfo{michaels.meeting/michaels.meeting.MeetUp}: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.os.Looper.loop(Looper.java:144)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread.main(ActivityThread.java:4937)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at java.lang.reflect.Method.invoke(Method.java:521)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at dalvik.system.NativeStart.main(Native Method)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383): Caused by: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.database.CursorWrapper.getString(CursorWrapper.java:140)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at michaels.meeting.MeetUp.onCreate(MeetUp.java:47)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
03-25 16:55:08.399: ERROR/AndroidRuntime(5383):     ... 11 more

The contact that it fails definitely has a number but after debugging I think that pCur doesnt have a column for numbers..Any ideas why this is happening? Thanks in advance -- mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丶情人眼里出诗心の 2024-11-02 13:34:10

我认为在开始枚举之前您可能需要调用 cursor.moveToFirst()

I think you may need a cursor.moveToFirst() call before you start enumerating.

2024-11-02 13:34:10

尝试使用此代码来检索联系人:

        ContentResolver cr1 = getContentResolver();
    String[] projection = new String[] {People._ID,People.NAME,People.NUMBER};
    Uri contacts =  People.CONTENT_URI;
    // Make the query. 
    Cursor managedCursor = cr1.query(contacts, projection, null, null, People.NAME + " ASC");
        if (managedCursor.moveToFirst()) {
            String contactname; 
            String cphoneNumber; 
            int nameColumn = managedCursor.getColumnIndex(People.NAME); 
            int phoneColumn = managedCursor.getColumnIndex(People.NUMBER);
            do {
                // Get the field values
                contactname = managedCursor.getString(nameColumn);
                cphoneNumber = managedCursor.getString(phoneColumn);
                if((cphoneNumber!= "" && cphoneNumber!=null)|| (contactname!="" && contactname!=null)){
                    c_Name.add(contactname);
                    c_Number.add(cphoneNumber);
                }
            } while (managedCursor.moveToNext());
        }

这将检索姓名和号码...

Try this code to Retrieve Contacts :

        ContentResolver cr1 = getContentResolver();
    String[] projection = new String[] {People._ID,People.NAME,People.NUMBER};
    Uri contacts =  People.CONTENT_URI;
    // Make the query. 
    Cursor managedCursor = cr1.query(contacts, projection, null, null, People.NAME + " ASC");
        if (managedCursor.moveToFirst()) {
            String contactname; 
            String cphoneNumber; 
            int nameColumn = managedCursor.getColumnIndex(People.NAME); 
            int phoneColumn = managedCursor.getColumnIndex(People.NUMBER);
            do {
                // Get the field values
                contactname = managedCursor.getString(nameColumn);
                cphoneNumber = managedCursor.getString(phoneColumn);
                if((cphoneNumber!= "" && cphoneNumber!=null)|| (contactname!="" && contactname!=null)){
                    c_Name.add(contactname);
                    c_Number.add(cphoneNumber);
                }
            } while (managedCursor.moveToNext());
        }

This will retreive the Name and Number ...

暗藏城府 2024-11-02 13:34:10

我发现问题是什么了! while 循环,特别是 moveToNext() 调用使其崩溃。如果您摆脱循环,则工作正常!

pCur.moveToFirst();
numbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

I found what the problem was! The while loop and specifically the moveToNext() call was making it crash..works fine if you get rid of the loop!

pCur.moveToFirst();
numbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文