gpt4 book ai didi

android - IllegalArgumentException 在 Xamarin Forms 应用程序中访问联系人

转载 作者:行者123 更新时间:2023-11-30 00:21:55 24 4
gpt4 key购买 nike

我正在尝试访问 Xamarin Forms 应用程序中的用户联系人。我已经编写了特定于平台的实现并使用 DependencyService 调用它们,这似乎工作正常。但是,当我测试 Android 实现时,出现错误,代码在以下代码处抛出 IllegalArgumentException:

var uri = ContactsContract.Contacts.ContentUri;
string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id,
ContactsContract.CommonDataKinds.StructuredName.GivenName,
ContactsContract.CommonDataKinds.StructuredName.FamilyName,
ContactsContract.CommonDataKinds.Phone.Number,
ContactsContract.CommonDataKinds.Email.Address};
var phoneLoader = new CursorLoader(Android.App.Application.Context, uri, projection, null, null, null);
var cursor = (Android.Database.ICursor)phoneLoader.LoadInBackground();

我还尝试通过以下方式获取光标:

var cursor = Forms.Context.ApplicationContext.ContentResolver.Query(uri, projection, null, null, null);

我还尝试了以下方法:

var phoneLoader = new CursorLoader(Android.App.Application.Context);
phoneLoader.SetProjection(projection);
phoneLoader.Uri = ContactsContract.Contacts.ContentUri;
var cursor = (Android.Database.ICursor)phoneLoader.LoadInBackground();

但这会引发完全相同的错误:

09-09 14:27:22.163 I/MonoDroid(10022): UNHANDLED EXCEPTION:
09-09 14:27:22.222 I/MonoDroid(10022): Java.Lang.IllegalArgumentException: Invalid column data2
09-09 14:27:22.223 I/MonoDroid(10022): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0
09-09 14:27:22.223 I/MonoDroid(10022): at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method) [0x00068] in <6cd960837cc24c26bab2a0a29b597627>:0
09-09 14:27:22.223 I/MonoDroid(10022): at Android.Runtime.JNIEnv.CallObjectMethod (System.IntPtr jobject, System.IntPtr jmethod) [0x0000e] in <06064416b46d4ffbb3484c957929a39f>:0
09-09 14:27:22.223 I/MonoDroid(10022): at Android.Content.CursorLoader.LoadInBackground () [0x00043] in <06064416b46d4ffbb3484c957929a39f>:0

它在这一行:

cursor = (Android.Database.ICursor)phoneLoader.LoadInBackground();

任何人都可以看到导致错误的上述代码有什么问题吗?

最佳答案

java.Lang.IllegalArgumentException: Invalid column data2

该列确实存在于您的数据库中,如果您没有将该列添加到称为投影图的东西,您将看到您看到的“无效列”错误。

在投影中,ContactsContract.CommonDataKinds 不是您正在查询的 Contacts 表的一部分。

access the users Contacts in a Xamarin Forms app

这是访问用户联系人的另一种解决方案:

//Get all the names of the contacts and their phone number
ContentResolver cr = ContentResolver;
ICursor cur = cr.Query(ContactsContract.Contacts.ContentUri,null, null, null, null);
if (cur.Count > 0)
{
while (cur.MoveToNext())
{
String id = cur.GetString(cur.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.Id));
String name = cur.GetString(cur.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.DisplayName));

if (cur.GetInt(cur.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.HasPhoneNumber)) > 0)
{
ICursor pCur = cr.Query(ContactsContract.CommonDataKinds.Phone.ContentUri,
null,
ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId + " = ?",
new String[] { id },
null);
while (pCur.MoveToNext())
{
String phoneNo = pCur.GetString(pCur.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number));
System.Diagnostics.Debug.WriteLine("Name: " + name + ", Phone No: " + phoneNo);
}
pCur.Close();
}
}
}

关于android - IllegalArgumentException 在 Xamarin Forms 应用程序中访问联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46131254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com