使用 ID 时 contact.save() 上的应用程序崩溃 - Apache Cordova
App Crashing on contact.save() when using ID - Apache Cordova
编辑:我已经在多个 API 和一个实际的 android 设备上尝试过这个,但它仍然在保存时崩溃。
我正在使用 Visual Studio 的 Apache Cordova 工具和 cordova 联系人插件 (v2.13) 在 android 上开发基于联系人的应用程序。我目前可以保存和检索联系人,但无法更新联系人。我正在使用 genymotion 模拟器。和 设备本机联系人列表。
您应该能够通过传入已存在的联系人 ID 来更新联系人。
我有联系人对象并且正在填充所有字段,但是当我尝试保存它时应用程序崩溃并且没有任何错误消息。
我知道这是 contacts v1.3 及更低版本中的一个问题,但已在他们的问题列表中标记为已解决。
Contact Issue Resolution Documentation
这是我的对象的屏幕截图以及完整的 AngularJS 代码
droidSync.controller('managerController', function ($scope) {
//Initialize model
$scope.contact = {};
// Create a new contact
$scope.createContact = function () {
// Contact Object
contact = navigator.contacts.create();
$scope.saveContact();
};
// Pick Contact from Device
$scope.editContact = function () {
contact = navigator.contacts.pickContact(function(contact){
console.log(contact);
//This only updates the text fields. It does not actually assign new values to the object
$scope.$apply(function () {
$scope.contact.firstName = contact.name.givenName;
$scope.contact.lastName = contact.name.familyName;
$scope.contact.mobileNo = contact.phoneNumbers[0].value;
$scope.contact.homeNo = contact.phoneNumbers[1].value;
$scope.contact.email = contact.emails[0].value;
$scope.contact.id = contact.id;
});
})
};
// Delete Contact from Device
$scope.deleteContact = function () {
//Delete Function Here
};
$scope.saveContact = function () {
var contact = navigator.contacts.create();
if ($scope.contact.id !== "undefined") {
contact.id = $scope.contact.id;
}
// Display Name and Email
contact.displayName = $scope.contact.firstName;
contact.nickname = $scope.contact.lastName;
var emails = [];
emails[0] = new ContactField('work', $scope.contact.email, true)
contact.emails = emails;
// Phone Numbers
var phoneNumbers = [];
phoneNumbers[0] = new ContactField('mobile', $scope.contact.mobileNo, true); // preferred number
phoneNumbers[1] = new ContactField('home', $scope.contact.homeNo, false);
contact.phoneNumbers = phoneNumbers;
// Names
var name = new ContactName();
name.givenName = $scope.contact.firstName;
name.familyName = $scope.contact.lastName;
contact.name = name;
// save to device
contact.save();
}
});
droidSync.controller('settingsController', function ($scope) {
});
结果是主脚本文件被加载到错误的位置...我在 <head>
标记中引用了它,它需要放在 cordova 引用末尾的位置<body>
<!-- DroidSync references -->
<!-- WHERE IT WAS -->
<link href="css/index.css" rel="stylesheet" />
<link href="css/ionicons.css" rel="stylesheet" />
<link href="Content/ionic.css" rel="stylesheet">
</head>
<!-- Cordova reference, this is added to your app when it's built. -->
<script src="app/scripts.js"></script> <!-- wHERE IT WAS SUPPOSED TO GO -->
<script src="cordova.js"></script>
<script src="scripts/platformOverrides.js"></script>
<script src="scripts/index.js"></script>
编辑:我已经在多个 API 和一个实际的 android 设备上尝试过这个,但它仍然在保存时崩溃。
我正在使用 Visual Studio 的 Apache Cordova 工具和 cordova 联系人插件 (v2.13) 在 android 上开发基于联系人的应用程序。我目前可以保存和检索联系人,但无法更新联系人。我正在使用 genymotion 模拟器。和 设备本机联系人列表。
您应该能够通过传入已存在的联系人 ID 来更新联系人。
我有联系人对象并且正在填充所有字段,但是当我尝试保存它时应用程序崩溃并且没有任何错误消息。
我知道这是 contacts v1.3 及更低版本中的一个问题,但已在他们的问题列表中标记为已解决。
Contact Issue Resolution Documentation
这是我的对象的屏幕截图以及完整的 AngularJS 代码
droidSync.controller('managerController', function ($scope) {
//Initialize model
$scope.contact = {};
// Create a new contact
$scope.createContact = function () {
// Contact Object
contact = navigator.contacts.create();
$scope.saveContact();
};
// Pick Contact from Device
$scope.editContact = function () {
contact = navigator.contacts.pickContact(function(contact){
console.log(contact);
//This only updates the text fields. It does not actually assign new values to the object
$scope.$apply(function () {
$scope.contact.firstName = contact.name.givenName;
$scope.contact.lastName = contact.name.familyName;
$scope.contact.mobileNo = contact.phoneNumbers[0].value;
$scope.contact.homeNo = contact.phoneNumbers[1].value;
$scope.contact.email = contact.emails[0].value;
$scope.contact.id = contact.id;
});
})
};
// Delete Contact from Device
$scope.deleteContact = function () {
//Delete Function Here
};
$scope.saveContact = function () {
var contact = navigator.contacts.create();
if ($scope.contact.id !== "undefined") {
contact.id = $scope.contact.id;
}
// Display Name and Email
contact.displayName = $scope.contact.firstName;
contact.nickname = $scope.contact.lastName;
var emails = [];
emails[0] = new ContactField('work', $scope.contact.email, true)
contact.emails = emails;
// Phone Numbers
var phoneNumbers = [];
phoneNumbers[0] = new ContactField('mobile', $scope.contact.mobileNo, true); // preferred number
phoneNumbers[1] = new ContactField('home', $scope.contact.homeNo, false);
contact.phoneNumbers = phoneNumbers;
// Names
var name = new ContactName();
name.givenName = $scope.contact.firstName;
name.familyName = $scope.contact.lastName;
contact.name = name;
// save to device
contact.save();
}
});
droidSync.controller('settingsController', function ($scope) {
});
结果是主脚本文件被加载到错误的位置...我在 <head>
标记中引用了它,它需要放在 cordova 引用末尾的位置<body>
<!-- DroidSync references -->
<!-- WHERE IT WAS -->
<link href="css/index.css" rel="stylesheet" />
<link href="css/ionicons.css" rel="stylesheet" />
<link href="Content/ionic.css" rel="stylesheet">
</head>
<!-- Cordova reference, this is added to your app when it's built. -->
<script src="app/scripts.js"></script> <!-- wHERE IT WAS SUPPOSED TO GO -->
<script src="cordova.js"></script>
<script src="scripts/platformOverrides.js"></script>
<script src="scripts/index.js"></script>