2012-06-20 60 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

很高興幫助:-) –

相關問題