【问题标题】:executing JS functions from a Phonegap Android Plugin从 Phonegap Android 插件执行 JS 函数
【发布时间】:2012-01-16 12:16:23
【问题描述】:

我正在使用 Phonegap v1.3 制作一个 Phonegap Android 应用。我为 phonegap 编写了一个插件,它从系统中获取一个特定的文件,然后执行一个 js 函数。
由于所有插件都扩展了Phonegap 类插件,我无法直接从那里执行js 函数。所以从插件的 execute 函数中,我执行 -->

PluginResult result = null;
Home home = new Home();
result = new PluginResult(Status.OK);
home.loadPage("pic.jpg");

Home 是我的主类,它扩展了 Droidgap。
在家里我有功能 loadPage

public void loadPage(String filePath){   
super.setIntegerProperty("loadUrlTimeoutValue", 60000);
super.loadUrl("javascript:navigator.somefunc(\""+filePath+"\");");  
}

问题是 DroidGap 的 setIntegerProperty 函数正在抛出 NPE。这是日志。

01-16 11:41:32.364: W/System.err(13162): java.lang.NullPointerException
01-16 11:41:32.364: W/System.err(13162):    at com.phonegap.DroidGap.setIntegerProperty(DroidGap.java:738)
01-16 11:41:32.364: W/System.err(13162):    at e2o.mobile.Home.loadSharePage(Home.java:120)
01-16 11:41:32.364: W/System.err(13162):    at com.phonegap.plugins.checkImage.CheckUploadImage.execute(CheckUploadImage.java:158)
01-16 11:41:32.364: W/System.err(13162):    at com.phonegap.api.PluginManager$1.run(PluginManager.java:150)
01-16 11:41:32.364: W/System.err(13162):    at java.lang.Thread.run(Thread.java:1019)

基本上在setIntegerProperty函数中——

public void setIntegerProperty(String name, int value) {
    this.getIntent().putExtra(name, value);
}

this.getIntent() 正在返回 null。 你能告诉我我错过了什么吗?或者在我的用例场景中,我如何执行那个js函数。如果我不使用

super.setIntegerProperty("loadUrlTimeoutValue", 60000, intent);

那么 loadUrl 也什么都不做。 JS 没有被调用,或者超时。

【问题讨论】:

    标签: android cordova phonegap-plugins


    【解决方案1】:

    这是因为您正在实例化一个新的 Home 对象。该对象是使用构造函数创建的,但未调用 onCreate 方法,因此不会发生很多正确的对象设置。这就是你得到 NullPointerException 的原因。

    您正在返回一个状态为 OK 的新 PluginResult,因此成功函数可以为您执行 navigator.somefunc(filePath) 方法。这样会容易很多。

    【讨论】:

    • 是的,我采用了错误的方法。成功函数应该执行js函数。现在工作正常。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2015-12-15
    • 2013-03-05
    • 1970-01-01
    • 2023-04-02
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多