1

我是 phonegap 插件的新手;我想写一个小的回声插件;但由于缺少 _cordovaNative,我收到此错误。在 file:///android_asset/www/js/cordova-2.3.0.js:1112 并且没有触发 echo 事件,我附上我的文件供您参考

这是我的 index.html `

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Login | Royce Apps</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0"/> 

    <link rel="stylesheet" href="css/loginstyle.css">   
    <script type="text/javascript" src="js/jquery-1.7.1.js"></script>


    <script type="text/javascript" src="js/cordova-2.3.0.js"></script>
   <script type="text/javascript" src="MyPlugin.js"></script> 
    <script>
  /*Downloading data from remote*/

    PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin("MyPlugin", new MyPlugin());
    PluginManager.addService("MyPlugin", "com.coldfumokeh.MyPlugin");
});




 </script>

<script type="text/javascript">
var onDeviceReady = function() {
document.getElementById("devready").innerHTML
= "Device ready.";
};
function plugintest() {

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


document.getElementById("devready").innerHTML
= "OnDeviceReady fired.";
myPlugin("This is an example");
}
}
</script>

</head>
<body>
<h2>My Plugin App</h2>
<p>Native Android Plugins.</p>
<p>
<input type="button" onClick="plugintest()"/>
<span id="devready">Device not ready.</span>
</p>
</body>
</html>

This is my Myplugin.js

function myPlugin(text) {
    cordova.define("cordova/plugin/Myplugin", function (require, exports, module) {

  var exec = require("cordova/exec");
  module.exports = {
   greeting: function (message, win, fail) { // change 'show' to what your action in your plugin is or want to be
    exec(win, fail, "MyPlugin", "greeting", [message]);
   }

  };


});

}

var myplugin = cordova.require("cordova/plugin/MyPlugin");
 myplugin.greeting(text, // change 'show' to what your action in your plugin is or want to be
  function(echoValue) {
alert(echoValue)},
  function(echoValue) {
   console.log("PhoneGap Plugin: MyPlugin: callback error");
  });

/*MyPlugin = function(){};
MyPlugin.prototype.greeting = function(
message, successCallback, errorCallback){
PhoneGap.exec(
successCallback,
errorCallback,
'MyPlugin',
'greeting',
[message]
);
};
*/

然后 Myplugin.java

    package com.coldfumokeh;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
@SuppressWarnings("deprecation")
public class Myplugin extends Plugin {

    @Override
    public PluginResult execute(String action, JSONArray data, String callbackId){
        PluginResult.Status status = PluginResult.Status.OK;
        String result = "";
        try {
            if ("greeting".equals(action)) {
            result = data.getString(0);
            if (result != null && result.length() > 0) {
            PluginResult pluginResult =
            new PluginResult(status, result);
            return pluginResult;
            } else {
            return new PluginResult(PluginResult.Status.ERROR);
            }
            } else {
            status = PluginResult.Status.INVALID_ACTION;
            }
            return new PluginResult(status, result);
            } catch (JSONException e) {
            return new
            PluginResult(PluginResult.Status.JSON_EXCEPTION);
            }

    }

}

Then my plugin.xml

<?xml version="1.0" encoding="utf-8"?>

<plugins>
    <plugin name="App" value="org.apache.cordova.App"/>
    <plugin name="Geolocation" value="org.apache.cordova.GeoBroker"/>
    <plugin name="Device" value="org.apache.cordova.Device"/>
    <plugin name="Accelerometer" value="org.apache.cordova.AccelListener"/>
    <plugin name="Compass" value="org.apache.cordova.CompassListener"/>
    <plugin name="Media" value="org.apache.cordova.AudioHandler"/>
    <plugin name="Camera" value="org.apache.cordova.CameraLauncher"/>
    <plugin name="Contacts" value="org.apache.cordova.ContactManager"/>
    <plugin name="File" value="org.apache.cordova.FileUtils"/>
    <plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
    <plugin name="Notification" value="org.apache.cordova.Notification"/>
    <plugin name="Storage" value="org.apache.cordova.Storage"/>
    <plugin name="Temperature" value="org.apache.cordova.TempListener"/>
    <plugin name="FileTransfer" value="org.apache.cordova.FileTransfer"/>
    <plugin name="Capture" value="org.apache.cordova.Capture"/>
    <plugin name="Battery" value="org.apache.cordova.BatteryListener"/>
    <plugin name="SplashScreen" value="org.apache.cordova.SplashScreen"/>
    <plugin name="Downloader" value="com.phonegap.plugins.downloader.Downloader"/>
    <plugin name="VideoPlayer" value="com.phonegap.plugins.video.VideoPlayer"/>
    <plugin name="PdfViewer" value="com.phonegap.plugins.pdfViewer.PdfViewer"/>
    <plugin name="StatusBarNotification" value="com.phonegap.plugins.statusBarNotification.StatusBarNotification"/>

    <plugin name="LoadingIndicator" value="com.phonegap.plugins.PGLoadingDialog" />  
<plugin name="barcode" value="com.phonegap.plugins.barcodescanner" />  
    <plugin name="MyPlugin" value="com.coldfumokeh.MyPlugin"/>
</plugins>

` 我的控制台

   05-07 09:14:54.601: I/dalvikvm(279): Could not find method android.webkit.WebView.<init>, referenced from method org.apache.cordova.CordovaWebView.<init>
05-07 09:14:54.601: W/dalvikvm(279): VFY: unable to resolve direct method 3552: Landroid/webkit/WebView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;IZ)V
05-07 09:14:54.601: D/dalvikvm(279): VFY: replacing opcode 0x70 at 0x0001
05-07 09:14:54.611: D/dalvikvm(279): VFY: dead code 0x0004-005b in Lorg/apache/cordova/CordovaWebView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;IZ)V
05-07 09:14:54.991: I/CordovaLog(279): config.xml missing, reverting to cordova.xml
05-07 09:14:54.991: I/CordovaLog(279): Changing log level to DEBUG(3)
05-07 09:14:55.001: I/CordovaLog(279): Found preference for classicRender=true
05-07 09:14:55.001: D/CordovaLog(279): Found preference for classicRender=true
05-07 09:14:55.151: D/JsMessageQueue(279): Set native->JS mode to 2
05-07 09:14:55.151: I/CordovaWebView(279): Disabled addJavascriptInterface() bridge since Android version is old.
05-07 09:14:55.172: D/DroidGap(279): DroidGap.init()
05-07 09:14:55.292: D/CordovaWebView(279): DroidGap.loadUrl(file:///android_asset/www/index.html, 5000)
05-07 09:14:55.292: D/DroidGap(279): onMessage(splashscreen,show)
05-07 09:14:55.301: D/CordovaWebView(279): >>> loadUrl(file:///android_asset/www/index.html)
05-07 09:14:55.301: D/PluginManager(279): init()
05-07 09:14:55.311: I/PluginManager(279): Using plugins.xml instead of config.xml.  plugins.xml will eventually be deprecated
05-07 09:14:55.361: D/CordovaWebView(279): >>> loadUrlNow()
05-07 09:14:55.361: D/DroidGap(279): Resuming the App
05-07 09:14:55.512: D/SoftKeyboardDetect(279): Ignore this event
05-07 09:14:55.601: D/SoftKeyboardDetect(279): Ignore this event
05-07 09:14:56.371: D/DroidGap(279): onMessage(onPageStarted,file:///android_asset/www/index.html)
05-07 09:14:57.122: D/dalvikvm(279): GC_FOR_MALLOC freed 2464 objects / 171192 bytes in 253ms
05-07 09:15:02.001: D/CordovaLog(279): module cordova/plugin/MyPlugin not found
05-07 09:15:02.001: E/Web Console(279): module cordova/plugin/MyPlugin not found at undefined:0
05-07 09:15:02.641: D/Cordova(279): onPageFinished(file:///android_asset/www/index.html)
05-07 09:15:02.641: D/DroidGap(279): onMessage(onNativeReady,null)
05-07 09:15:02.652: D/DroidGap(279): onMessage(onPageFinished,file:///android_asset/www/index.html)
05-07 09:15:03.171: I/Database(279): sqlite returned: error code = 14, msg = cannot open file at source line 25467
05-07 09:15:03.731: D/CordovaLog(279): Failed to run constructor: ReferenceError: Can't find variable: MyPlugin
05-07 09:15:03.731: I/Web Console(279): Failed to run constructor: ReferenceError: Can't find variable: MyPlugin at file:///android_asset/www/js/cordova-2.3.0.js:304
05-07 09:15:03.781: D/CordovaLog(279): 1
05-07 09:15:03.781: I/Web Console(279): 1 at file:///android_asset/www/js/cordova-2.3.0.js:1110
05-07 09:15:03.791: D/CordovaLog(279): Falling back on PROMPT mode since _cordovaNative is missing.
05-07 09:15:03.791: I/Web Console(279): Falling back on PROMPT mode since _cordovaNative is missing. at file:///android_asset/www/js/cordova-2.3.0.js:1112
05-07 09:15:03.861: D/CordovaNetworkManager(279): Connection Type: 3g
05-07 09:15:03.891: D/CordovaNetworkManager(279): Connection Type: 3g
05-07 09:15:03.891: D/DroidGap(279): onMessage(networkconnection,3g)
05-07 09:15:04.041: D/DroidGap(279): onMessage(spinner,stop)
05-07 09:15:04.731: D/DroidGap(279): onMessage(spinner,stop)

先感谢您; 等待任何帮助

4

2 回答 2

1

Cordova 2.0 删除了插件使用的“addPlugin”方法。所以一个快速的解决方法是删除(或注释掉)用于添加插件的“addConstructor”函数,并将其替换为窗口对象的显式附件:

//cordova.addConstructor(function() {
// cordova.addPlugin('plugin', new pluginname());
//});
window.plugin = new plugin();
于 2013-05-07T15:31:26.250 回答
1

看看我的博客,了解我是如何定义插件的。绝对需要,正如“白痴​​”所说,不是冒犯,这只是他的绰号。

另外:将该脚本加载为所有脚本中的第一个。

我的博客: Android PhoneGap Toast 插件

看这部分:

    cordova.define("cordova/plugin/toasts", function (require, exports, module) {
 var exec = require("cordova/exec");
 module.exports = {
  showShort: function (message, win, fail) {
   exec(win, fail, "Toasts", "show_short", [message]);
  },
  showLong: function (message, win, fail) {
   exec(win, fail, "Toasts", "show_long", [message]);
  },
  cancel: function (win, fail) {
   exec(win, fail, "Toasts", "cancel", []);
  }
 };
});

而这部分:

    function toast(text,duration) {
 var toasts = cordova.require("cordova/plugin/toasts");
 if(duration=="short") {
  toasts.showShort(text,
  function() {
   //console.log("PhoneGap Plugin: Toast short: callback success");
  },
  function() {
   console.log("PhoneGap Plugin: Toast short: callback error");
  });
 } else if(duration=="long") {
  toasts.showLong(text,
  function() {
   //console.log("PhoneGap Plugin: Toast long: callback success");
  },
  function() {
   console.log("PhoneGap Plugin: Toast long: callback error");
  });
 } else {
  toasts.cancel(
  function() {
   //console.log("PhoneGap Plugin: Toast cancel: callback success");
  },
  function() {
   console.log("PhoneGap Plugin: Toast cancel: callback error");
  });
 }
}

用您自己的替换插件名称(toasts),不要忘记必须用您自己的替换的命令(showShort 等)。

另请查看文档:PhoneGap docs 2.7.0

于 2013-05-09T15:00:01.210 回答