创建phonegap应用程序后已经出错,无法调用方法方法'querySelector'

jhd*_*hdj 7 android cordova

在我创建了一个phonegap应用程序并运行它之后,似乎在logcat中出现了错误

Uncaught TypeError: Cannot call method 'querySelector' of null at file:///android_asset/www/js/index.js:41
Run Code Online (Sandbox Code Playgroud)

这是一个问题吗?我该如何解决这个问题?

che*_*tie 26

我有这个错误,我想你已经尝试做我做的事情.我开始从示例应用程序代码构建应用程序,而不太了解事件模型以及何时触发.

示例应用程序在index.html中包含HTML部分

        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
Run Code Online (Sandbox Code Playgroud)

这是由JS中的事件触发更新的内容:

    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
   //...  
   // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        app.receivedEvent('deviceready');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
Run Code Online (Sandbox Code Playgroud)

但是我用ID"deviceready"取出了div,所以JS因你描述的错误而失败.我删除了调用app.receivedEvent ...一切正常.已经说过把那个div放回去导致了一个不同的最大调用堆栈错误所以我没有卖掉我仍然有我的JS正确.

我(想想!)我也学到了不要试图使用经典的$(document).ready(function()来加载你的应用程序功能.这应该是一段从代码中调用的函数而是触发了onDeviceReady事件.