javascript 未捕获的类型错误:对象 #<Object> 在 file:///android_asset/www/index.html 没有方法“exec”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13554251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:08:29  来源:igfitidea点击:

Uncaught TypeError: Object #<Object> has no method 'exec' at file:///android_asset/www/index.html

javascriptandroidcordovaphonegap-plugins

提问by javadevg

  • using PhoenGap 2.2.0
  • Executed \bin\create C:\Temp\Test com.test Test
  • Had following output
  • 使用 PhoenGap 2.2.0
  • 执行\bin\create C:\Temp\Test com.test Test
  • 有以下输出

Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved.

Microsoft (R) Windows Script Host 版本 5.8 版权所有 (C) Microsoft Corporation。版权所有。

Creating new android project...
Building jar and js files...
Copying template files...
Copying js, jar & config.xml files...
Copying cordova command tools...
Updating AndroidManifest.xml and Main Activity...
  • Imported Project
    • On eclipse (4.2.0) did
    • File > New > Project > Android > Android Project from Existing Code
    • Selected the folder C:\Temp\Test
    • Checked "Copy project into workspace"
  • Making changes
    • Checked res\xml\config.xml and the line <plugin name="Notification" value="org.apache.cordova.Notification"/>is present
    • Checked that index.html has the cordova-2.2.0.js included
    • Case#1 Opened index.html and modified from
  • 导入项目
    • 在日食(4.2.0)上做了
    • 文件 > 新建 > 项目 > Android > 来自现有代码的 Android 项目
    • 选择文件夹 C:\Temp\Test
    • 选中“将项目复制到工作区”
  • 做出改变
    • 检查 res\xml\config.xml 并且该行<plugin name="Notification" value="org.apache.cordova.Notification"/>存在
    • 检查 index.html 是否包含cordova-2.2.0.js
    • Case#1 打开 index.html 并修改自

<script type="text/javascript"> app.initialize(); </script>

<script type="text/javascript"> app.initialize(); </script>

to

<script type="text/javascript">
function showAlert(msg){
navigator.notification.alert(msg);
}
document.addEventListener("deviceready", showAlert('You are the winner!'), false);
app.initialize();
</script>

I get following error 11-25 10:29:58.399: E/Web Console(14604): Uncaught TypeError: Cannot call method 'alert' of undefined at file:///android_asset/www/index.html:40

我收到以下错误 11-25 10:29:58.399: E/Web Console(14604): Uncaught TypeError: Cannot call method 'alert' of undefined at file:///android_asset/www/index.html:40

    • Case#2 Opened index.html and modified from
    • Case#2 打开 index.html 并修改自

<script type="text/javascript"> app.initialize(); </script>

<script type="text/javascript"> app.initialize(); </script>

to

<script type="text/javascript">
function successAlert(){}
function errorAlert(){}
function showAlert(msg){
cordova.exec(successAlert, errorAlert, "Notification","alert", [msg]);
}
document.addEventListener("deviceready", showAlert('You are the winner!'), false);
app.initialize();
</script>

I get following error 11-25 10:25:06.575: E/Web Console(14149): Uncaught TypeError: Object #<Object> has no method 'exec' at file:///android_asset/www/index.html:42}

我收到以下错误 11-25 10:25:06.575: E/Web Console(14149): Uncaught TypeError: Object #<Object> has no method 'exec' at file:///android_asset/www/index.html:42}

I'm sure that I missed something...just that I'm not able to conclude what is it. Please help me out.

我确定我错过了一些东西......只是我无法得出结论它是什么。请帮帮我。

回答by AndiDog

This will call showAlertimmediately, instead of delaying to when the event fires:

这将showAlert立即调用,而不是延迟到事件触发时:

document.addEventListener("deviceready", showAlert('You are the winner!'), false)

Instead do this

而是这样做

document.addEventListener("deviceready", function() {
    showAlert('You are the winner!')
}, false)