onDeviceReady没有加载

时间:2014-07-16 09:39:54

标签: android cordova

我是手机差距的新手。我在提到http://docs.phonegap.com/的例子。     但是当我试图执行http://docs.phonegap.com/en/1.8.1/cordova_contacts_contacts.md.html#Contacts中给出的程序时     onDeviceReady函数没有加载。

**Case 1:**
**Here is the code(index.html file):**

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

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

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

        // Cordova is ready
        //
        function onDeviceReady() {
        alert("onDeviceReady");
         console.log("onDeviceReady");
            // create
            var contact = navigator.contacts.create();
            contact.displayName = "Plumber";
            contact.nickname = "Plumber";       //specify both to support all devices
            var name = new ContactName();
            name.givenName = "Jayshree";
            name.familyName = "Barkur";
            contact.name = name;

            // save
            contact.save(onSaveSuccess,onSaveError);

            // clone
            //var clone = contact.clone();
           // clone.name.givenName = "John";
           // console.log("Original contact name = " + contact.name.givenName);
            //console.log("Cloned contact name = " + clone.name.givenName); 

            // remove
            //contact.remove(onRemoveSuccess,onRemoveError);
        }

        // onSaveSuccess: Get a snapshot of the current contacts
        //
        function onSaveSuccess(contact) {
            alert("Save Success");
        }

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

        // onRemoveSuccess: Get a snapshot of the current contacts
        //
        function onRemoveSuccess(contacts) {
            alert("Removal Success");
        }

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

        </script>
      </head>
      <body>
        <h1>Example</h1>
        <p>Create Contacts</p>

      </body>
    </html>

I am executing this program in eclipse(Android 4.4).On execution the emmulator only shows 
**Examples**
**Create Contacts**
And nothing happens after that i.e the the onDeviceReady method is not called and neither the other callback functions are called.

**Case 2:**
When i am adding an extra line in the program i.e. <p><a href="#" onclick="onDeviceReady();">Add Contact</a></p> in the body section,The contacts are getting added.
But 
function onSaveSuccess(contact) {
        alert("Save Success");
    }
does not works for me.
**Here goes my code(index.html file):**

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

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

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

        // Cordova is ready
        //
        function onDeviceReady() {
        alert("onDeviceReady");
         console.log("onDeviceReady");
            // create
            var contact = navigator.contacts.create();
            contact.displayName = "Plumber";
            contact.nickname = "Plumber";       //specify both to support all devices
            var name = new ContactName();
            name.givenName = "Jayshree";
            name.familyName = "Barkur";
            contact.name = name;

            // save
            contact.save(onSaveSuccess,onSaveError);

            // clone
            //var clone = contact.clone();
           // clone.name.givenName = "John";
           // console.log("Original contact name = " + contact.name.givenName);
            //console.log("Cloned contact name = " + clone.name.givenName); 

            // remove
            //contact.remove(onRemoveSuccess,onRemoveError);
        }

        // onSaveSuccess: Get a snapshot of the current contacts
        //
        function onSaveSuccess(contact) {
            alert("Save Success");
        }

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

        // onRemoveSuccess: Get a snapshot of the current contacts
        //
        function onRemoveSuccess(contacts) {
            alert("Removal Success");
        }

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

        </script>
      </head>
      <body>
        <h1>Example</h1>
        <p>Create Contacts</p>
        <p><a href="#" onclick="onDeviceReady(); return false;">Add Contact</a></p>
      </body>
    </html>

Kindly let me know the reason behind this and suggest some solution to solve this problem.
Thanks in advance

2 个答案:

答案 0 :(得分:0)

好像你没有添加cordova库 从here下载相同内容并将cordova-1.8.0.js添加到www文件夹

答案 1 :(得分:0)

试试这个:

启用仿真器GPU。

在Eclipse中转到Window -> AVD Manager select your AVD and click "Edit"

检查&#34; Use Host GPU&#34;框,然后保存并重新启动AVD。

希望这有帮助。