IllegalArgument 接受来自联系人的 onActivityResult?
我使用此方法返回结果代码,然后从联系人数据库中获取联系人信息。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case (CONTACT_PICKER_RESULT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
break;
}
ContactInfo.setText("Contact: " + name + "./n" + " Phone Number: "+ phoneNumber);
}
}
}
问题是我不断收到此错误。
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/data/229 flg=0x1 (has extras) }} to activity {com.fttech.test2/com.fttech.test2.TestContacts}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): at android.database.CursorWindow.getString_native(Native Method)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): at android.database.CursorWindow.getString(CursorWindow.java:375)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
I am using this method to return a result code and then get a contacts information from the contacts database.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case (CONTACT_PICKER_RESULT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
break;
}
ContactInfo.setText("Contact: " + name + "./n" + " Phone Number: "+ phoneNumber);
}
}
}
The problem is i keep getting this error.
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/data/229 flg=0x1 (has extras) }} to activity {com.fttech.test2/com.fttech.test2.TestContacts}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): at android.database.CursorWindow.getString_native(Native Method)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): at android.database.CursorWindow.getString(CursorWindow.java:375)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
10-13 17:27:19.017: ERROR/AndroidRuntime(6781): at android.database.CursorWrapper.getString(CursorWrapper.java:135)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的问题出在其中的一些
cursor.getColumnIndex
上,也许ContactsContract.Contacts._ID
就是问题所在。但我给您的建议是逐一调试,并监视您在该代码中拥有的所有
ContactsContract.*
访问。返回 -1 的人就是给你带来问题的人。
I think your problem is located in some of that
cursor.getColumnIndex
, maybeContactsContract.Contacts._ID
is the problem ones .But my tip for you is debug one by one putting a watch on all
ContactsContract.*
access you have in that code .The ones who returns a -1 is the one who is giving you the problems.