gpt4 book ai didi

android - 适用于 Android 的 Phonegap 插件无法正常工作。为什么?

转载 作者:行者123 更新时间:2023-11-29 21:56:53 25 4
gpt4 key购买 nike

我无法使 Android phonegap 插件正常工作。甚至没有一个是我发现的例子,也不是我试图自己创造一个的可悲失败。我首先尝试使用像 this one 这样的教程.他们不为我工作。我总是以 Cannot call method of undefined 错误结束。

所以我尝试了一些现成的东西。得到 this project来自github。它只是一个简单的插件来显示 toast 。我检查了我在教程中学到的所有内容:

//the package name in the java
package com.phonegap.toast;

//my class extends Plugin and has a simple show toast method.

public class Tutorial extends Plugin {
@Override
public PluginResult execute(String cmd, JSONArray args, String callback) {
if(cmd.equals("toast"))
{
return showToast(args);
}
return null;
}

private PluginResult showToast(JSONArray args) {
final String message;
try {
message = args.getString(0);
ctx.runOnUiThread(new Runnable()
{
public void run() {
Toast myToast = Toast.makeText(ctx, message, Toast.LENGTH_SHORT);
myToast.show();
}
});

return new PluginResult(PluginResult.Status.OK);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}

}

插件定义在res/xml/plugins.xml

插件名称="Tutorial"value="com.phonegap.toast.Tutorial"

不,如果我把它放在 rex/xml/config.xml 上它也不起作用

最后是调用插件的方法:

function createToast() {
// i also tried window.Tutorial.showToast('Hello AndroidOpen'); with no success
window.plugins.Tutorial.showToast('Hello AndroidOpen');
}

这里我又遇到了同样的错误。

10-22 15:39:07.770: E/Web 控制台 (2885): 未捕获类型错误: 无法调用文件中未定义的方法“showToast”:///android_asset/www/main.js:123

任何开明的灵魂都可以向我解释我做错了什么?我已经尝试了好几天了,有很多不同的插件,包括我自己的,甚至是 this ones我不知道它是什么。

最佳答案

好吧,这里有一些可能会让您感到困惑的事情。首先,如果 config.xml 文件存在于 res/xml 中,那么它将优先于 plugins.xml。因此,您需要将插件行添加到 config.xml。

确保为 Toast 插件包含 .js 文件。

第三,window.plugins 已被弃用,因此如果您使用的是 PhoneGap 2.0.0 或更高版本,您可能需要修改 .js。查看我的 blog post on the topic .根本的变化是您现在需要在 JS 中添加新的 PluginName,因为默认情况下它不再放在 window.plugins 中。

关于android - 适用于 Android 的 Phonegap 插件无法正常工作。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13015017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com