Am developing a android application using phonegap(Cordova 2.0.0). I need the retrieve the device contacts. I have tried with code given this doc
http://docs.phonegap.com/en/2.0.0/cordova_contacts_contacts.md.html#Contacts
my code as follows
$("#shareoptions3").live('click',function(){
var options = new ContactFindOptions();
options.multiple = true;
var fields = ["displayName","phoneNumbers"];//["displayName", "name","phoneNumbers"];
navigator.contacts.find(fields, onContactSuccess, onContactError, options);
});
function onContactSuccess(contacts) {//alert(contacts.length);
for (var i=0; i<contacts.length; i++) {
// display phone numbers
for (var j=0; j<contacts[i].phoneNumbers.length; j++) {
alert("Type: " + contacts[i].phoneNumbers[j].type + "\n" +
"Value: " + contacts[i].phoneNumbers[j].value + "\n" +
"Preferred: " + contacts[i].phoneNumbers[j].pref);
}
}
};
// onError: Failed to get the contacts
//
function onContactError(contactError) {
console.log('Error in getting contacts!');
}
I am getting message in Logcat like this :
Error in success callback: Contacts3 = TypeError: Cannot read property ‘length’ of null at file:///android_asset/www/JS/cordova-2.0.0.js:258
Please help me to sort out this problem.
One of your contact may not have a telephone number, and that is why you’ll get a
nullvalue instead of a phone number.So, in your
forloop, one of thecontacts[i].phoneNumbers.lengthwil generate an error.I suggest you to first check if the
phoneNumbersisnullor not, before displaying / alerting it, by using:In the end, you may try something like this:
Hope this helps, let me know if this works for you.