Phonegap - 删除联系人使我的应用程序无法正常工作

时间:2014-04-18 15:00:06

标签: cordova contacts

我开发了一个简单的phonegap应用来管理联系人。

此部分列出所有联系人和按钮,单击此按钮时,删除联系人。一切正常但是当我成功删除一个联系人时,我想重新加载我的网页以查看新列表而不删除联系人但我的应用程序无法正确加载。

我不知道问题是什么......

代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Contact Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        // specify contact search criteria
        var options = new ContactFindOptions();
        options.filter = "";      // empty search string returns all contacts
        options.multiple = true;  // return multiple results
        filter = ["displayName","phoneNumbers"]; // return contact.displayName field    displayName

        // find contacts
        navigator.contacts.find(filter, onSuccess, onError, options);
    }

    // onSuccess: Get a snapshot of the current contacts
    //
    function onSuccess(contacts) {

      for (i=0;i<contacts.length;i++) {
        //window.alert("ID = " + contacts[i].id);
        document.write(contacts[i].displayName);
        tel = contacts[i].phoneNumbers;
        if (tel== null){
document.write("Sense telefon");
        }else{
                for (j=0;j<contacts[i].phoneNumbers.length;j++) {
            if (contacts[i].phoneNumbers != null &&
contacts[i].phoneNumbers != 'undefined') {
                document.write(" "+contacts[i].phoneNumbers[j].value);
            }
        }
        }
        //document.write('<button id='+i+' onClick="reply_click(this.id,contacts);">Borrar</button>');
        var button = document.createElement("button"); 
        button.id = i; 
        button.onclick = function() { reply_click(this.id, contacts); }; // append button to DOM
        document.body.appendChild(button);
        document.write(contacts[i].id);
        document.write("<br/>");
    }
    };

    function reply_click(clicked_id,contacts)
    {

        window.alert(clicked_id);
        window.alert(contacts);
         for (i=0;i<contacts.length;i++) {
         if(i==clicked_id){
         try{
            persona = contacts[i];
            persona.remove(onRemoveSuccess,onRemoveError);
            window.alert("Contacte Borrat");
            setTimeout("location.href='buscacont.html'", 5000);
            }catch(err){
            window.alert(err);
            }
            }
         }

    }
    function onError(contactError) {
        window.alert('onError!');
    }
    function onRemoveSuccess(contacts) {
         window.alert("Removal Success");
    }

    // onRemoveError: Failed to get the contacts
    //
    function onRemoveError(contactError) {
         window.alert("Error = " + contactError.code);
    }


    </script>

  </head>
  <body>
    <h1>Example</h1>
    <p>Find Contacts</p>
  </body>
</html>

日志没有说任何有用的东西。

编辑:ADDITTIONAL INFO

我的其他页面有这个代码,它有不同的结构:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {

    //function prova(){

    //}
    /*
        app.receivedEvent('deviceready');
        var myContact = navigator.contacts.create({"displayName": "Test User"});
        myContact.note = "This contact has a note.";
        console.log("The contact, " + myContact.displayName + ", note: " + myContact.note);
        app.receivedEvent('contactcreated');
        myContact.save(app.onContactSaveSuccess,app.onContactSaveError);
        */
    },
    // Update DOM on a Received Event

    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');
        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
    onContactSaveSuccess: function(contact) {
        window.alert("Save Success");
    },
    onContactSaveError: function(contact) {
        window.alert("Save Failed");
    },

};

function doFunction(){
window.alert("Saaaaaaaaaaaaaaaaaaave Success");
var myContact = navigator.contacts.create({"displayName": "Test LELO"});
        myContact.note = "This contact has a note.";
        console.log("The contact, " + myContact.displayName + ", note: " + myContact.note);
        app.receivedEvent('contactcreated');
        myContact.save(app.onContactSaveSuccess,app.onContactSaveError);
        window.alert("Saaaaaaaaaaaaaaaaaaave Success");
}

function creaContacte()
{
var x = document.getElementById("frm1");
var txt = "";
  nom = x.elements[0].value;
  num= x.elements[1].value;
  email= x.elements[2].value;

        var myContact = navigator.contacts.create({"displayName": nom});
        myContact.note = "Contacte creat amb MarcGarciaContactManager";
        myContact.name = nom;

        var phoneNumbers = [];
        phoneNumbers[0] = new ContactField('General', num, true);
        myContact.phoneNumbers = phoneNumbers;


        var emails = [];
        emails[0] = new ContactField('General', email, true);
        myContact.emails = emails;

        console.log("The contact, " + myContact.displayName + ", note: " + myContact.note);

        myContact.save(app.onContactSaveSuccess,app.onContactSaveError);
        window.alert("Contact creat");

}
function buscacon()
{
 document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //
    function onDeviceReady() {
        // find all contacts with 'Bob' in any name field
        var options = new ContactFindOptions();
        options.filter="pepepe"; 
        var fields = ["displayName", "name"];
        navigator.contacts.find(fields, onSuccess, onError, options);
    }

    // onSuccess: Get a snapshot of the current contacts
    //
    function onSuccess(contacts) {
        for (var i=0; i<contacts.length; i++) {
            window.alert("Display Name = " + contacts[i].displayName);
        }
    }

    // onError: Failed to get the contacts
    //
    function onError(contactError) {
        alert('onError!');
    }


}

这是一个添加联系人的简单代码,我没有添加它的html部分,因为它是一个简单的表单和一个javascript调用函数...

奇怪的是,当我运行应用程序.....如果首先使用此页面创建联系人,我可以一直检查没有问题的其他页面(该页面可以查看联系人),但是如果我首先检查页面以查看联系人,第二次访问时崩溃(没有正确加载设备或其他......)

1 个答案:

答案 0 :(得分:0)

第二次可能不会触发ondeviceready事件。 像这样更改代码。 删除document.addEventListener("deviceready", onDeviceReady, false);行并将其添加回名为onLoad(或您想要的)的函数中:

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

然后,使用<body>更改<body onload="onLoad()">

上面列出的代码来自official documentation

这应该有效,因为

  

在deviceready事件触发后注册的任何事件处理程序都会立即调用其回调函数。

所以,即使deviceready事件已被触发并且第二次没有再次触发,这样无论如何都会调用回调。