Я пробовал количество комбинаций, но при поиске номера телефона я вижу ошибку. Вот мой код:
public void readPhoneLookUp(){
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//people.moveToFirst();
while(people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
Log.i(LOG, "Contacts: "+ contact);
int numberFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = people.getString(numberFieldColumnIndex);
Log.i(LOG, "Number: "+ number);
}
}//readPhoneLookUp ends
Я также добавил разрешение READCONTACTS в Манифесте, который у меня есть.также пробовал это:
public void readContactsNew(){
ArrayList<String> contactListArrLst = null;
contactListArrLst = new ArrayList<String>();
ArrayList<String> contactNumberArrLst = null;
contactNumberArrLst = new ArrayList<String>();
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
int curCount = cursor.getCount();
Log.i(LOG, "Contacts:Count "+ curCount);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
contactListArrLst.add(cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME)));
contactNumberArrLst.add(cursor.getString(cursor.getColumnIndex(PhoneLookup.NUMBER)));
cursor.moveToNext();
}
cursor.close();
Log.i(LOG, "Contacts: "+ contactListArrLst);
Log.i(LOG, "ContactNumber: "+ contactNumberArrLst);
Но выдают ошибку в contactNumberArrLst
Это ошибка в журнале Cat:
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): FATAL EXCEPTION: main
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at android.database.CursorWindow.getString_native(Native Method)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at android.database.CursorWindow.getString(CursorWindow.java:361)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at com.ravi.conproviderdemo1.ContentProviderDemo1Activity.readPhoneLookUp(ContentProviderDemo1Activity.java:63)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at com.ravi.conproviderdemo1.ContentProviderDemo1Activity$MyOnClickListener.onClick(ContentProviderDemo1Activity.java:74)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at android.view.View.performClick(View.java:2408)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at android.view.View$PerformClick.run(View.java:8816)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at android.os.Handler.handleCallback(Handler.java:587)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at android.os.Looper.loop(Looper.java:123)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at android.app.ActivityThread.main(ActivityThread.java:4633)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at java.lang.reflect.Method.invoke(Method.java:521)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-11 10:39:44.231: ERROR/AndroidRuntime(1683): at dalvik.system.NativeStart.main(Native Method)
11-11 10:39:44.231: WARN/ActivityManager(174): Force finishing activity com.ravi.conproviderdemo1/.ContentProviderDemo1Activity
11-11 10:39:44.401: WARN/AudioFlinger(95): write blocked for 166 msecs, 384 delayed writes, thread 0xbb50