【问题标题】:Unable to interact with anything in Cordova app无法与 Cordova 应用程序中的任何内容进行交互
【发布时间】:2016-10-18 12:11:54
【问题描述】:

我无法从我的第一个 Cordova 项目中获得任何形式的交互性。我有 HTML、Javascript 和 CSS 方面的经验,所以我认为 Cordova 将是混合应用程序开发的完美介绍,但我似乎连一个基本的 HTML 按钮都无法在应用程序上工作。

我使用的是安装了 Android Studio 和 Cordova 的 Windows 10。我想我已经正确设置了项目文件结构。我一直在关注 Cordova 的文档,并在所有设备类型上安装了地理定位插件。为了进行测试,我同时使用了 Android Studio 中的虚拟 android 设备和 android 手机 (OnePlus X),它已正确连接(每当我在控制台中输入“cordova run android”时,应用程序就会在手机上打开)。

我开始尝试获取当前位置,然后将坐标显示为警报。这不起作用,所以为了尝试更简单的方法,我添加了一个按钮,单击时应该显示一个警报弹出窗口,但两者都不起作用。这是我当前的代码:

www/index.html

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>  
        <div class="app">
            <h1 id="headertext">My first Cordova App!</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received" id="geolocation">Device is ready!</p>
            </div>
            <br>
            <div>
            <button type="button" onclick="basicAlertTest();">Click Me!</button>
            </div>
        </div>

        <script type="text/javascript" charset="utf-8">
            // onSuccess Callback
            // current GPS coordinates
            var onSuccess = function(position) {
                alert('Latitude: '          + position.coords.latitude          + '\n' +
                      'Longitude: '         + position.coords.longitude         + '\n' +
                      'Altitude: '          + position.coords.altitude          + '\n' +
                      'Accuracy: '          + position.coords.accuracy          + '\n' +
                      'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
                      'Heading: '           + position.coords.heading           + '\n' +
                      'Speed: '             + position.coords.speed             + '\n' +
                      'Timestamp: '         + position.timestamp                + '\n');
            };

            // onError Callback receives a PositionError object
            function onError(error) {
                alert('code: '    + error.code    + '\n' +
                      'message: ' + error.message + '\n');
            }

            // Listening for the device to be ready
            document.addEventListener("deviceready", onDeviceReady, false);
            function onDeviceReady() {
                console.log("navigator.geolocation should be able to run successfully now...");
                navigator.geolocation.getCurrentPosition(onSuccess, onError);
            }

            // Checking to see if a basic alert will appear on click of "Click me!" button
            function basicAlertTest(){
            alert("This is the alert test, button works!");
            }
        </script>

        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>

    </body>
</html> 

我缺少一些基本的东西吗?我以为我可以像往常一样用 HTML/Javascript/CSS 进行编码,但需要学习一些代码行来集成使用本机设备功能(如地理位置或相机)的插件。我已经确保安装了地理定位插件(使用cordova plugin add cordova-plugin-geolocation),但没有出现警报。我需要一个单独的插件来显示警报/与屏幕交互吗?

另外,我注意到每当我进行虚拟测试时,如果我查看“扩展控件”中的位置,它给出的坐标是:

Longitude: -122.0840 Latitude: 37.4220 Altitude: 0.0

这是加利福尼亚州山景城,离我大约 8,000 英里¯_(ツ)_/¯

截图如下:

我正在本地测试,这有什么不同吗?我真的很感激任何帮助或建议,我可能错过了一些非常明显的东西。提前谢谢!

更新 1 “cordova plugin ls”返回的已安装插件列表:

cordova-plugin-compat 1.0.0 "Compat" cordova-plugin-dialogs 1.3.0 "Notification" cordova-plugin-geolocation 2.4.0 "Geolocation" cordova-plugin-whitelist 1.3.0 "Whitelist"

更新 2

onSuccess 函数应该在设备准备好后运行。当它运行时,它应该显示一个包含位置详细信息的警报,但由于我收到“设备准备就绪”消息而没有出现任何警报,所以我决定添加一个按钮来手动调用该函数。

我现在确定onSuccess 在设备准备就绪时无法正常运行,因为每当我单击按钮使其手动运行时,控制台中都会显示以下错误:

Uncaught TypeError: Cannot read property 'coords' of undefined

我很困惑,因为我使用的代码与此处显示的相同:https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-geolocation/index.html

另外,我注意到我正在处理的索引模板包含 &lt;script type="text/javascript" src="cordova.js"&gt;&lt;/script&gt;,但是当我查看包含我的索引文件的文件夹时,没有 cordova.js 文件,它不在那里,我在cordova下载里好像找不到,这是正常的还是需要单独下载?

最新尝试:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src * gap://ready file:; style-src 'self' 'unsafe-inline' *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; img-src * data: 'unsafe-inline'">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>

        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </head>
    <body>  
        <div class="app">
            <h1 id="headertext">My first Cordova App!</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received" id="geolocation">Device is ready!</p>
            </div>
            <br>
            <div>
            <button type="button" onclick="basicAlertTest();">Click Me!</button>
            <button type="button" onclick="onSuccess();">Run onSuccess function</button>
            </div>
        </div>

        <script type="text/javascript" charset="utf-8">

                   // onSuccess Callback
            // current GPS coordinates
            var onSuccess = function(position) {
                alert('Latitude: '          + position.coords.latitude          + '\n' +
                      'Longitude: '         + position.coords.longitude         + '\n' +
                      'Altitude: '          + position.coords.altitude          + '\n' +
                      'Accuracy: '          + position.coords.accuracy          + '\n' +
                      'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
                      'Heading: '           + position.coords.heading           + '\n' +
                      'Speed: '             + position.coords.speed             + '\n' +
                      'Timestamp: '         + position.timestamp                + '\n');
            };



            // onError Callback receives a PositionError object
            function onError(error) {
                alert('code: '    + error.code    + '\n' +
                      'message: ' + error.message + '\n');
            }

            // Listening for the device to be ready
            document.addEventListener("deviceready", onDeviceReady, false);
            function onDeviceReady() {
                console.log("navigator.geolocation should be able to run successfully now...");
                navigator.geolocation.getCurrentPosition(onSuccess, onError);
            }

            // Checking to see if a basic alert will appear on click of "Click me!" button
            function basicAlertTest(){
            console.log("This is the alert test, button works!");
            alert("This is the alert test, button works!");
            }
        </script>

    </body>
</html>

【问题讨论】:

  • 请显示您的控制台
  • 嗨@ProllyGeek 我已经在我的问题底部更新了最新代码和控制台错误:Uncaught TypeError: Cannot read property 'coords' of undefined 在此先感谢您提供任何帮助。
  • 我检查了您的评论,该按钮现在可以使用,这是个好消息,坏消息是某些 Cordova 功能在模拟器上无法使用,您需要一个真实的设备来完成此操作(取决于插件)对于地理定位,模拟器的插件将依靠 html5 位置而不是传感器位置m 如果您有任何我们可以交流的工具,有很多要解释的,我也许可以帮助解决您的问题。
  • 嗨@ProllyGeek 感谢您的评论,问题现在已解决,但我有兴趣了解在不同设备上使用地理定位插件可能出现的问题,如果您有 Skype 帐户,那么我可以加你。
  • 很高兴您解决了您的问题,您可以随时在 Skype 上添加 prollygeek,干杯。

标签: javascript android node.js cordova


【解决方案1】:

删除 index.html 文件顶部的这一行

<meta http-equiv="Content-Security-Policy" content="default-src * gap: ws: https://ssl.gstatic.com;style-src * 'unsafe-inline' 'self' data: blob:;script-src * 'unsafe-inline' 'unsafe-eval' data: blob:;img-src * data: 'unsafe-inline' 'self' content:;fmedia-src mediastream;">

【讨论】:

    【解决方案2】:

    尝试使用此内容 - 安全 - 策略它应该可以工作。

    &lt;meta http-equiv="Content-Security-Policy" content="default-src * gap: ws: https://ssl.gstatic.com;style-src * 'unsafe-inline' 'self' data: blob:;script-src * 'unsafe-inline' 'unsafe-eval' data: blob:;img-src * data: 'unsafe-inline' 'self' content:;fmedia-src mediastream;"&gt;

    【讨论】:

    • 无法识别的内容安全策略指令'fmedia-src'。
    【解决方案3】:

    添加额外条件以检查您的应用是否正确安装了地理定位插件。所以,它会像,

    if(navigator.geolocation){
      console.log('Geolocation is there');
      navigator.geolocation.getCurrentPosition(function(position) {
        alert('Latitude: '          + position.coords.latitude          + '\n' +
                      'Longitude: '         + position.coords.longitude         + '\n' +
                      'Altitude: '          + position.coords.altitude          + '\n' +
                      'Accuracy: '          + position.coords.accuracy          + '\n' +
                      'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
                      'Heading: '           + position.coords.heading           + '\n' +
                      'Speed: '             + position.coords.speed             + '\n' +
                      'Timestamp: '         + position.timestamp                + '\n');
      } , function(error) {
        console.log('Error while connecting to geolocation ' + error);
      }, {timeout:10000});
    } else {
      console.log('Geolocation is not there');
    }
    

    【讨论】:

    • 嗨@Hgoon 谢谢,我正在尝试你的建议,但没有任何改变,我不知道如何使用虚拟 android 设备查看控制台,所以我看不到任何 console.logs,是有办法查看吗?此外,当我在浏览器中打开页面时,应用程序卡在“连接到设备”上,因此控制台中没有任何内容出现,即使我点击“点击我!”按钮。当我输入cordova plugin ls 时,地理定位插件出现在列表中。我已将完整列表添加到问题的末尾。有没有办法让我查看虚拟设备的控制台?再次感谢。
    • 我明白了。首先,要使 console.log 工作,您可能需要一个名为 cordova-plugin-console 的插件。但在你拥有这个插件之前,我认为你应该尝试替换所有 console.log 以在浏览器上发出警报并运行它(只是为了清楚地理定位是否有效)。以防万一,请将 cordova.js 放在首位。请确保您的 index.js 不会干扰您的代码。祝你好运!
    • 再次嗨@Hgoon,我已经在我的笔记本电脑上使用Chrome设置了一个控制台,从中我可以看到我的手机发生了什么,当我打开应用程序时它显示以下错误:@ 987654323@
    • 另外,当我在浏览器中打开它时没有显示警报,这可能是由于它卡在“连接到设备”上,也许在它检测到设备之前什么都不会运行准备好了吗?
    • 你是对的,大多数cordova插件在'deviceready'状态之后运行。但是,除非应用程序正在加载沉重的东西,否则它仍然不需要时间来达到状态。关于内容安全策略,你想试试我的吗?我的适用于 Android 5,6 和 iOS 10。 抱歉帮不上忙,因为我在 OS X 中开发 iOS 和 Android环境。不熟悉虚拟设备。
    猜你喜欢
    • 2020-08-09
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多