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

Posted

技术标签:

【中文标题】Android - 获取所有联系人列表时出错【英文标题】:Android - error in getting list of all contacts 【发布时间】:2015-06-16 00:06:36 【问题描述】:

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

MainActivity 的 onCreate 中的代码

        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 ComponentInfocom.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

【问题讨论】:

在这里查看我的答案***.com/a/26820544/2252830 【参考方案1】:
try 
Log.d(TAG, "Contacts: " + people.getCount());
        people.moveToFirst();
        while (!people.isAfterLast()) 
            String number = people.getString(indexNumber);
         people.add(number);
        
     catch (Exception ignored) 
    

【讨论】:

【参考方案2】:

代码在这里:

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);

【讨论】:

以上是关于Android - 获取所有联系人列表时出错的主要内容,如果未能解决你的问题,请参考以下文章

在列表视图中填充电话联系人时出错

Android 获取联系人列表未在 Android 10 中显示

获取android联系人电话号码列表

Android从联系人列表中获取电话号码

在 Android 中高效处理动态列表视图项

获取属于特定组的联系人列表