简体   繁体   English

Cordova 6.3.1 android app javascript无法触发

[英]Cordova 6.3.1 android app javascript not firing

I've been banging my head against the wall, if anyone could help, that would be super appreciated! 我一直在撞墙,如果有人可以帮忙,那将非常感激!

Both my js files are in www/js. 我的两个js文件都在www / js中。 The cordova.js file was generated for me. cordova.js文件是为我生成的。

The "e" in body was just to make sure my new code was getting there. 正文中的“ e”只是为了确保我的新代码到达那里。 It is, I've changed the letter in dozens of builds while trying to get this to work, just to make sure, and it always changes. 是的,为了确保它能正常运行,我已经更改了数十个构建版本中的字母,并且它总是在变化。

index.html 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 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 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();

When I fire up the app, I get no alerts, and volume down does nothing. 当我启动该应用程序时,没有任何警报,而调低音量也无济于事。 This is what I get in the console when I build the apk 这是我构建APK时在控制台中得到的

BUILD SUCCESSFUL

Total time: 4.479 secs
Built the following apk(s):
        /root/hello/platforms/android/build/outputs/apk/android-debug.apk
root@0SfCordova:~/hello# cordova -v
6.3.1

Also, I installed node and cordova on a blank ubuntu 14.04 digital ocean box using just the command line. 另外,我仅使用命令行在空白的ubuntu 14.04数字海洋盒上安装了node和cordova。 Where do I go to see console logs? 我在哪里可以看到控制台日志? Thanks! 谢谢! I have no idea how to debug in this environment. 我不知道如何在这种环境中进行调试。

Remove Onload event from html page.And don't write cordova.js file.Cordova will automatically create this file.You can use following code: 从html页面中删除Onload事件,不要编写cordova.js文件,Cordova会自动创建此文件,您可以使用以下代码:

index.html 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 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');
}

i came across the same error, it could not work on the emulator. 我遇到了相同的错误,它无法在模拟器上运行。 Here is the solution that worked for me that i think will also work for you. 这是对我有用的解决方案,我认为也对您有用。 I am running the latest cordova 6.3.1 on my MAC OS 10.12. 我在我的MAC OS 10.12上运行最新的cordova 6.3.1。

i removed this line of code from my html files. 我从我的html文件中删除了这一行代码。 I am still trying to understand why it worked after removing the below line. 在删除以下行后,我仍在尝试了解它为何起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM