创建Phonegap插件会导致调用方法'Action'未定义

时间:2012-05-05 14:56:14

标签: android cordova phonegap-plugins native-code

我有一个小问题。我正在尝试创建一个插件来做一些沉重的事情 在原生插件中工作,并且还在限制中运输 web sqlite实现。 (数据库限制为5MB,但我需要更多)。

首先,这是用于完成工作的Main类:

package com.worker.api;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;

public class worker extends Plugin{
    public final String version = "1.0";
    public String callback;
    public worker(){

    }

    public PluginResult execute(String action, JSONArray args, String
                                callbackId) {
        this.callback = callbackId;
        PluginResult r = null;
        try{
            if(action.equals("connectDB")){

            }else if(action.equals("closeDB")){

            }else if(action.equals("query")){
                r = this.query();
            }else if(action.equals("insert")){
                r = this.insert();
            }else if(action.equals("update")){
                r = this.update();
            }else if(action.equals("delete")){
                r = this.delete();
            }else if(action.equals("syncronize")){
                r = this.syncronize();
            }else if(action.equals("getVersion")){
                r = this.getVersion();
            }else{
                r = new PluginResult(Status.INVALID_ACTION);
                r.setKeepCallback(true);
            }
        }catch(JSONException ex){
            r = new PluginResult(Status.JSON_EXCEPTION);
        }
        return r;
    }
    public PluginResult query(){
        return null;
    }
    public PluginResult insert(){
        return null;
    }
    public PluginResult update(){
        return null;
    }
    public PluginResult delete(){
        return null;
    }
    public PluginResult syncronize(){
        return null;
    }
    public PluginResult getVersion() throws JSONException{
        JSONObject obj = new JSONObject();
        obj.put(version,this.version);
        return new PluginResult(Status.OK,obj);
    }
}

目前我只实现了getVersion部分,因为我还在 在此进行测试

在plugin.xml中我添加了这个:

<plugin name="workerAPI" value="com.worker.api.worker" />

主js文件如下所示:

    var workerApi = function(){}

    var genericSuccess = function(e) {
        alert("Success: " + e.toString());
    };

    var fail = function(e) {
        alert("Failure " + e.toString());
    };

    workerApi.prototype.doAction = function(
            operation,
            argument,
            successFunction,
            failFunction
            ){
        var succ = successFunction ? successFunction : genericSuccess;
        var faic = failFunction ? failFunction : fail;
        return PhoneGap.exec(
                succ,
                faic,
                "workerAPI",
                operation,
                [argument]
        );
    };

    PhoneGap.addConstructor(function() {
        PhoneGap.addPlugin(
                    "workApi",
                    new workerApi()
        );
    });

    var WKapi = {
            "openDB": function(){
                window.plugins.workApi.doAction(
                    "connectDB",
                    null
                );
            },
            "closeDB": function(){
                window.plugins.workApi.doAction(
                    "closeDB",
                    null
                );
            },
            "query": function(arg){
                window.plugins.workApi.doAction(
                    "query",
                    arg
                );
            },
            "insert": function(arg){
                window.plugins.workApi.doAction(
                    "insert",
                    arg
                );
            },
            "update": function(arg){
                window.plugins.workApi.doAction(
                    "update",
                    arg
                );
            },
            "delete": function(arg){
                window.plugins.workApi.doAction(
                    "delete",
                    arg
                );
            },
            "sync": function(arg){
                window.plugins.workApi.doAction(
                    "syncronize",
                    arg
                );
            },
            "version": function(){
                window.plugins.workApi.doAction(
                    "version"
                );
            }
    };

一旦我执行它,记录器LogCat就会告诉我错误:

未捕获的TypeError:无法调用未定义的methid'doAction' 文件:///android_asset/www/worker.js:79

我真的不知道我做错了什么。

----- ------ ADDON

这是作为index.php

一部分的代码块
        <script lang="text/javascript">
            document.addEventListener("deviceready", onDeviceReady, false);
            var isRun = false;
            function onDeviceReady() {
                WKapi.version();
            };
        </script>
    </div>
</body>
</html>

我仍然得到Uncaught TypeError:无法调用方法'doAction'... 它发生在这个电话上:window.plugins.workerApi.doAction

如果我将window.plugins.workerApi放入警报中,它会让我知道回来。

1 个答案:

答案 0 :(得分:0)

在您的Java类中,您正在寻找“getVersion”,因此需要在此处传递。

"version": function(){
    window.plugins.workApi.doAction(
        "getVersion"
        );
    }