2016-08-20 161 views
0

我一直在撞牆,如果有人可以幫助,我會感激不盡!科爾多瓦6.3.1 android應用程序的JavaScript不解僱

我的js文件都在www/js中。 cordova.js文件是爲我生成的。

身體中的「e」只是爲了確保我的新代碼到達那裏。它是,我已經改變了幾十個版本的信件,同時試圖讓這個工作,只是爲了確保,並且它總是變化。

的index.html

<!DOCTYPE html> 
<html> 
<head> 
<title>Device Ready Example</title> 

<script type="text/javascript" charset="utf-8" src="cordova.js"></script> 
<script type="text/javascript" charset="utf-8" src="example.js"></script> 
</head> 
<body onload="onLoad()"> 
    e 
</body> 
</html> 

example.js

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

    // device APIs are available 
// 
function onDeviceReady() { 
    alert('yo'); 
    document.addEventListener("volumedownbutton", onVolumeDown, false); 
    // Add similar listeners for other events 
} 

function onVolumeDown() { 
    alert('hey'); 
    document.body.innerHTML += '<div style="position:absolute;width:100%;height:100%;opacity:0.3;z-index:100;background:#000;">asdfs</div>'; 

} 

cordova.js

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 explicitly call 'app.receivedEvent(...);' 
    onDeviceReady: function() { 
     app.receivedEvent('deviceready'); 
    }, 
    // 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); 
    } 
}; 

app.initialize(); 

當我火起來的應用程序,我沒有得到任何警告,而成交量下降呢沒有。這是我在控制檯中看到,當我建立

BUILD SUCCESSFUL 

Total time: 4.479 secs 
Built the following apk(s): 
     /root/hello/platforms/android/build/outputs/apk/android-debug.apk 
[email protected]:~/hello# cordova -v 
6.3.1 

另外,我安裝的節點和科爾多瓦在僅使用命令行空白的Ubuntu 14.04數字海洋框中APK。我去哪裏看控制檯日誌?謝謝!我不知道如何在這個環境下進行調試。

+0

你碰巧命名你的項目'科爾多瓦'?應該加載一個真正的'cordova.js'文件。你的文件被認爲是'main.js',因爲它不包含cordova js框架。 – gro

+0

不,它的名字叫你好,因爲我正在關注hello world tutorial的官方文檔。 – user2278120

+0

@ user2278120你在android設備上測試它嗎? – Gandhi

回答

0

從刪除Onload事件HTML page.And不寫cordova.js file.Cordova會自動創建該file.You可以使用下面的代碼:

的index.html

<!DOCTYPE html> 
<html> 
<head> 
<title>Device Ready Example</title> 

<script type="text/javascript" charset="utf-8" src="cordova.js"></script> 
<script type="text/javascript" charset="utf-8" src="example.js"></script> 
</head> 
<body> 
    e 
</body> 
</html> 

example.js

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

function onDeviceReady() { 
    alert('yo'); 
document.addEventListener("volumedownbutton", onVolumeDown, false); 
// Add similar listeners for other events 
} 
function onVolumeDown() { 
    alert('hey'); 
} 
+0

Homen,謝謝你的嘗試,但我嘗試過,並沒有得到警報。是否因爲我正在測試實際的Android設備上的apk文件? – user2278120

+0

我只爲實際的Android設備寫的。我相信它會工作。只使用上面的代碼。沒有其他的。它會工作 – Homen

+0

我確實嘗試了這一點。不知道爲什麼它不起作用。 – user2278120

-1

我遇到了同樣的錯誤,它無法在模擬器上工作。這是爲我工作的解決方案,我認爲它也適用於您。我在MAC OS 10.12上運行最新的cordova 6.3.1。

我從我的html文件中刪除了這行代碼。我仍然試圖瞭解爲什麼它刪除下面的行後工作。

+0

什麼?您是否測試OP代碼以查看它是否修復? – kabanus

+0

我看到該行無法發佈。我做的是刪除js文件夾中的項目隨附的index.js。我也從我的所有html頁面中刪除了它的鏈接。看來這是阻止我整個設備準備就緒的問題。爲什麼,因爲從我的所有頁面中刪除它後,即使從真實設備而不是仿真器運行應用程序,deviceready現在也可以啓動。你應該嘗試一下,看看。 –

相關問題