Android 应用程序读取联系人电话号码时出现问题
我试图读取联系人的电话号码,但无法让它工作...
我的代码与 此处
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为在开始枚举之前您可能需要调用
cursor.moveToFirst()
。I think you may need a
cursor.moveToFirst()
call before you start enumerating.尝试使用此代码来检索联系人:
这将检索姓名和号码...
Try this code to Retrieve Contacts :
This will retreive the Name and Number ...
我发现问题是什么了! while 循环,特别是 moveToNext() 调用使其崩溃。如果您摆脱循环,则工作正常!
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!