插件未在Android中的PhoneGap 3.4中定义错误

时间:2014-06-01 15:20:24

标签: android cordova

我正在尝试编写自己的插件,使用phonegap 3.4从html文件调用另一个活动。但是在customplugin.js文件中面临很多问题。

我一直在努力,但无法纠正它。它工作在1.6版本。但是我想开始使用phonegap 3.4。

以下是我正在使用的代码。

的index.html

<!DOCTYPE html>
<html>
  <head>
  <title></title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta charset="utf-8">

    <!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>

    <script type="text/javascript" charset="utf-8" src="customplugin.js"></script>
    <script type="text/javascript">


    function onBodyLoad()
    {       
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    /* When this function is called, Cordova has been initialized and is ready to roll */
    /* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
    see http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
    for more details -jm */
    function onDeviceReady()
    {
        // do your thing!
        alert("Cordova is working")
    }

    function callNativePlugin( returnSuccess ) {
        a=10;
        CustomPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
    }

    function nativePluginResultHandler (result) {
       alert("SUCCESS: \r\n"+result );
    }

    function nativePluginErrorHandler (error) {
       alert("ERROR: \r\n"+error );
    }



    </script>
    </head>
    <body onload="onBodyLoad()">
        <h1>Hey, it's Cordova!</h1>

        <button onclick="callNativePlugin('success');">Click to invoke the Native Plugin with an SUCCESS!</button>
        <button onclick="callNativePlugin('error');">Click to invoke the Native Plugin with an ERROR!</button>
    </body>
</html>

这是customplugin.js文件。

cordova.define("cordova/plugin/cordovaplugin", 
  function(require, exports, module) {

var CustomPlugin = {

    callNativeFunction: function (success, fail, resultType) {
        return cordova.exec(success, fail, "CustomPlugin", "nativeAction", [resultType,"hello"]);
    }
};

});

这是CordovaPlugin.java文件。

package com.phonegap.helloworld;


import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class CustomPlugin extends CordovaPlugin
{



    public PluginResult execute(String action, JSONArray data, String callbackId) {


        //
        startPhotoEditActivity();
        PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
        mPlugin.setKeepCallback(true);
        return mPlugin;
    }



    public void startPhotoEditActivity() {
        Intent myIntent = new Intent(this.cordova.getActivity(),SecActivity.class);       
        ((Activity) this.cordova).startActivity(myIntent);
    }
}

以下是我在config.xml文件中添加插件的方法

<feature name="CustomPlugin">
        <param name="android-package" value="com.phonegap.helloworld.CustomPlugin" />
</feature>

最后这是我得到的错误。

06-01 20:20:37.579: D/CordovaLog(28742): file:///android_asset/www/index.html: Line 33 :            Uncaught ReferenceError: CustomPlugin is not defined
06-01 20:20:37.579: I/chromium(28742): [INFO:CONSOLE(33)] "Uncaught ReferenceError: CustomPlugin is not defined", source: file:///android_asset/www/index.html (33)

在互联网上长时间搜索后,我无法找到解决方案。

2 个答案:

答案 0 :(得分:0)

您是否在customplugin.js的第一行尝试了“customplugin”而不是“cordovaplugin”

答案 1 :(得分:0)

根本原因是:v3.4引入了一些缺陷以防止插件正常工作。

以下是v3.4中未知插件错误的类似示例:

03-25 18:21:03.810: D/PluginManager(7273): exec() call to unknown plugin: AdMob

最终通过将cordova从v3.4降级到v3.3来解决问题。

由于缺陷已在v3.5中修复,因此您最好更新到v3.5。