Bug 27392 - Xamarin.Mobile 0.7.4 (Android component) - Issues accessing AddressBook
Summary: Xamarin.Mobile 0.7.4 (Android component) - Issues accessing AddressBook
Alias: None
Product: Components
Classification: Xamarin
Component: Xamarin Components ()
Version: Production (addons.xamarin.com)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bill Holmes
Depends on:
Reported: 2015-02-25 03:41 UTC by Shane Raiteri
Modified: 2015-06-04 11:21 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Shane Raiteri 2015-02-25 03:41:59 UTC
Hi devs,

Currently having issues on a particular device (HTC One M7) accessing the AddressBook with the Xamarin.Mobile API in Android. The code is set up in the following way:

AddrBook = new AddressBook(_context) { PreferContactAggregation = false };
AddrBook.RequestPermission().ContinueWith(t =>
                 if (!t.Result) {
                    //Failure path

                    var nativeContacts = AddrBook.Distinct().OrderBy(p => p.DisplayName).ToList();
                    //Failure path

On the line where nativeContacts are being resolved, it times out after 10-15 seconds with the following exception:

 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
  at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00064] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:195 
  at Android.Content.ContentResolver.Query (Android.Net.Uri uri, System.String[] projection, System.String selection, System.String[] selectionArgs, System.String sortOrder) [0x0004b] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Content.ContentResolver.cs:790 
  at Xamarin.Contacts.ContactHelper+<GetContacts>d__18.MoveNext () [0x000c8] in c:\BuildAgent\work\bb79242ef4f930a5\MonoDroid\Xamarin.Mobile\Contacts\ContactHelper.cs:126 
  at Xamarin.Contacts.ContactHelper+<GetContacts>d__9.MoveNext () [0x00109] in c:\BuildAgent\work\bb79242ef4f930a5\MonoDroid\Xamarin.Mobile\Contacts\ContactHelper.cs:82 
  at Xamarin.Contacts.ContactReader+<GetEnumerator>d__4.MoveNext () [0x002ed] in c:\BuildAgent\work\bb79242ef4f930a5\MonoDroid\Xamarin.Mobile\Contacts\ContactReader.cs:99 
  at System.Collections.Generic.List`1[Xamarin.Contacts.Contact].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0 
  at System.Collections.Generic.List`1[Xamarin.Contacts.Contact]..ctor (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.ToList[Contact] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
  at BankApp.Common.Lib.BaseContactService.LoadContacts () [0x0006a] in /Users/digitalbank/Desktop/Projects/banknativeapp/Common/BankApp.Common.Lib/Services/BaseContactService.cs:70 
  --- End of managed exception stack trace ---
java.lang.IllegalArgumentException: the bind value at index 9 is null
	at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
	at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
	at android.content.ContentProviderProxy.query(ContentProviderNative.java:463)
	at android.content.ContentResolver.query(ContentResolver.java:476)
	at android.content.ContentResolver.query(ContentResolver.java:419)
	at dalvik.system.NativeStart.run(Native Method)

A little confused why it is happening on this device alone, but wondering if there is something contact related that can be deduced from the error exception?

Comment 1 Bill Holmes 2015-06-04 11:21:03 UTC
I am thinking the problem is with your Linq query.  For my device I was getting exceptions on the OrderBy part.  I believe that I have contacts with a null DisplayName.

For me changing the statement to the avoided the crash.

var nativeContacts = addrBook.Distinct().
    OrderBy(p => string.IsNullOrEmpty(p.DisplayName) ? "" : p.DisplayName)

If this does not resolve the issue I suggest trying the statement in parts;

var nativeContacts = AddrBook.ToList ();

var nativeContacts2 = AddrBook.Distinct ().ToList ();

var nativeContacts3 = addrBook.Distinct().
    OrderBy(p => string.IsNullOrEmpty(p.DisplayName) ? "" : p.DisplayName)

See which one gives you trouble.