2012-06-20 31 views
0

我拥有人们可以想象的最简单的PhoneGap应用程序!使用HTML 5进行设备准备事件时出现PhoneGap错误

我想要做的就是在deviceready事件上显示警报消息。

HTML代码

<!DOCTYPE HTML> 
<html> 
<head> 
    <title>PhoneGap</title> 
    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
    <script type="text/javascript" charset="utf-8" src="common.js"></script> 
</head> 
<body> 
    <div data-role="page" id="index-page"> 
     <h1>Hello World!</h1> 
</body> 
</html> 

common.js CODE

var isPhoneGapReady = false; 
function init() { 

     document.addEventListener("deviceready", 
      onDeviceReady, false); 

     // Older versions of Blackberry < 5.0 don't support 
     // PhoneGap's custom events, so instead we need to 
     // perform an interval check every 500 milliseconds 
     // to see if PhoneGap is ready. Once done, the 
     // interval will be cleared and normal processing 
     // can begin 
     var intervalID = window.setInterval(function() { 
       if (PhoneGap.available) { 
        onDeviceReady(); 
       } 
      }, 500); 
    } 

function onDeviceReady() { 
    window.clearInterval(intervalID); 

    // set to true 
    isPhoneGapReady = true; 
alert("The device is now ready"); 
} 

// Set an onload handler to call the init function 
window.onload = init; 

我使用云服务来获取APK文件,我运行它的Android模拟器版本4.0中。 3。

错误控制台上:

init 
Ignote this event 
W/webcore(6387): java.lang.Throwable: EventHub.removeMessages(int what = 107) is not supported before the WebViewCore is set up. 
at android.webkit.WebViewCore$EventHub.removeMessages(WebViewCore.java:1683) 

,我将不胜感激,如果有人可以请指出需要做纠正错误的东西。

感谢,

回答

1

我想你遇到的问题是,你的intervalID的范围没有达到你的onDeviceReady()功能。您需要将init()函数内创建一个功能,像这样 -

var isPhoneGapReady = false; 

function init() { 

    document.addEventListener("deviceready", onDeviceReady, false); 

    // Older versions of Blackberry < 5.0 don't support 
    // PhoneGap's custom events, so instead we need to 
    // perform an interval check every 500 milliseconds 
    // to see if PhoneGap is ready. Once done, the 
    // interval will be cleared and normal processing 
    // can begin 

    var intervalID = window.setInterval(function() { 
      if (PhoneGap.available) { 
       onDeviceReady(); 
      } 
     }, 500); 

     // REMOVE THIS 
     // } 

    function onDeviceReady() { 
     window.clearInterval(intervalID); 

     // set to true 
     isPhoneGapReady = true; 
    alert("The device is now ready"); 
    } 

// PUT THIS HERE 
} 

// Set an onload handler to call the init function 
window.onload = init; 
+0

devicereqady在window.onload之前是不可能发生的。 – user655577

+0

这个工程!谢谢Jasper – user655577

+0

很高兴帮助:-) –

相关问题