问题
my html code in /assets/www
<!DOCTYPE html>
<\html>
<\head>
<\title>FileReader Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.6.0.js"></script>
<script type="text/javascript" charset="utf-8">
function onLoad() {
console.log("in onLoad");
document.addEventListener("deviceready", onDeviceReady, false);
execute(success, fail, "Echo", action, [args,"1","2"]);
console.log("in at end of onLoad");
}
function onDeviceReady() {
console.log("in onDeviceReady");
}
function success(file){
console.log("success");
}
function fail(evt) {
console.log("in fail");
console.log(evt.target.error.code);
}
<\/script>
<\/head>
<\body onload="onLoad()" >
<\h1>Example</h1>
<\p>Read File</p>
<\/body>
<\/html>
My java class code
package com.example.testphonegap;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
public class Echo extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
// if (action.equalsIgnoreCase("echo")) {
String message = args.getString(0);
this.echo(message, callbackContext);
Log.v("success", "success");
return true;
// }
// return false;
}
private void echo(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
callbackContext.success(message);
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
}
line added in config.xml in /res/xml
<plugin name="Echo" value="com.example.testphonegap.Echo" />
My Logcat
04-15 12:22:38.455: I/dalvikvm(778): Could not find method org.apache.cordova.CordovaWebView.setOverScrollMode, referenced from method org.apache.cordova.DroidGap.init
04-15 12:22:38.455: W/dalvikvm(778): VFY: unable to resolve virtual method 1098: Lorg/apache/cordova/CordovaWebView;.setOverScrollMode (I)V
04-15 12:22:38.455: D/dalvikvm(778): VFY: replacing opcode 0x6e at 0x0043
04-15 12:22:38.615: I/CordovaLog(778): Changing log level to DEBUG(3)
04-15 12:22:38.635: I/CordovaLog(778): Found preference for useBrowserHistory=true
04-15 12:22:38.655: D/CordovaLog(778): Found preference for useBrowserHistory=true
04-15 12:22:38.655: I/CordovaLog(778): Found preference for exit-on-suspend=false
04-15 12:22:38.665: D/CordovaLog(778): Found preference for exit-on-suspend=false
04-15 12:22:38.665: D/DroidGap(778): DroidGap.onCreate()
04-15 12:22:38.725: I/dalvikvm(778): Could not find method android.webkit.WebView.<init>, referenced from method org.apache.cordova.CordovaWebView.<init>
04-15 12:22:38.725: W/dalvikvm(778): VFY: unable to resolve direct method 344: Landroid/webkit/WebView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;IZ)V
04-15 12:22:38.725: D/dalvikvm(778): VFY: replacing opcode 0x70 at 0x0001
04-15 12:22:38.745: D/dalvikvm(778): VFY: dead code 0x0004-0046 in Lorg/apache/cordova/CordovaWebView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;IZ)V
04-15 12:22:39.465: D/JsMessageQueue(778): Set native->JS mode to 2
04-15 12:22:39.475: I/CordovaWebView(778): Disabled addJavascriptInterface() bridge since Android version is old.
04-15 12:22:39.485: E/dalvikvm(778): Could not find class 'android.webkit.WebResourceResponse', referenced from method org.apache.cordova.CordovaWebViewClient.getWhitelistResponse
04-15 12:22:39.525: W/dalvikvm(778): VFY: unable to resolve new-instance 129 (Landroid/webkit/WebResourceResponse;) in Lorg/apache/cordova/CordovaWebViewClient;
04-15 12:22:39.525: D/dalvikvm(778): VFY: replacing opcode 0x22 at 0x000b
04-15 12:22:39.525: D/dalvikvm(778): VFY: dead code 0x000d-0014 in Lorg/apache/cordova/CordovaWebViewClient;.getWhitelistResponse ()Landroid/webkit/WebResourceResponse;
04-15 12:22:39.585: W/dalvikvm(778): VFY: unable to find class referenced in signature (Landroid/webkit/WebResourceResponse;)
04-15 12:22:39.615: W/dalvikvm(778): VFY: unable to find class referenced in signature (Landroid/webkit/WebResourceResponse;)
04-15 12:22:39.615: W/dalvikvm(778): VFY: unable to find class referenced in signature (Landroid/webkit/WebResourceResponse;)
04-15 12:22:39.665: D/DroidGap(778): DroidGap.init()
04-15 12:22:39.696: D/CordovaWebView(778): >>> loadUrl(file:///android_asset/www/library_access.html)
04-15 12:22:39.725: D/PluginManager(778): init()
04-15 12:22:39.765: D/CordovaWebView(778): >>> loadUrlNow()
04-15 12:22:39.775: D/CordovaWebView(778): >>> loadUrl(file:///android_asset/www/file_reader.html)
04-15 12:22:39.775: D/PluginManager(778): init()
04-15 12:22:39.805: D/CordovaWebView(778): >>> loadUrlNow()
04-15 12:22:39.805: D/DroidGap(778): Resuming the App
04-15 12:22:39.915: D/DroidGap(778): onMessage(onPageStarted,file:///android_asset/www/library_access.html)
04-15 12:22:39.955: D/SoftKeyboardDetect(778): Ignore this event
04-15 12:22:40.084: D/Cordova(778): onPageFinished(file:///android_asset/www/library_access.html)
04-15 12:22:40.084: D/Cordova(778): Trying to fire onNativeReady
04-15 12:22:40.095: D/DroidGap(778): onMessage(onNativeReady,null)
04-15 12:22:40.095: D/DroidGap(778): onMessage(onPageFinished,file:///android_asset/www/library_access.html)
04-15 12:22:40.134: D/DroidGap(778): onMessage(onPageStarted,file:///android_asset/www/file_reader.html)
04-15 12:22:40.225: D/SoftKeyboardDetect(778): Ignore this event
04-15 12:22:41.184: D/dalvikvm(778): GC_FOR_MALLOC freed 2668 objects / 187648 bytes in 208ms
04-15 12:22:42.185: D/DroidGap(778): onMessage(spinner,stop)
04-15 12:22:44.655: D/CordovaLog(778): in onLoad
04-15 12:22:44.655: I/Web Console(778): in onLoad at file:///android_asset/www/file_reader.html:10
04-15 12:22:44.695: D/CordovaLog(778): ReferenceError: Can't find variable: execute
04-15 12:22:44.695: E/Web Console(778): ReferenceError: Can't find variable: execute at file:///android_asset/www/file_reader.html:12
04-15 12:22:47.615: D/CordovaLog(778): Falling back on PROMPT mode since _cordovaNative is missing. Expected for Android 3.2 and lower only.
04-15 12:22:47.615: I/Web Console(778): Falling back on PROMPT mode since _cordovaNative is missing. Expected for Android 3.2 and lower only. at file:///android_asset/www/cordova-2.6.0.js:906
04-15 12:22:47.935: D/CordovaNetworkManager(778): Connection Type: 3g
04-15 12:22:47.985: D/CordovaNetworkManager(778): Connection Type: 3g
04-15 12:22:47.995: D/DroidGap(778): onMessage(networkconnection,3g)
04-15 12:22:48.096: D/DroidGap(778): onMessage(spinner,stop)
04-15 12:22:48.134: D/Cordova(778): onPageFinished(file:///android_asset/www/file_reader.html)
04-15 12:22:48.134: D/Cordova(778): Trying to fire onNativeReady
04-15 12:22:48.145: D/DroidGap(778): onMessage(onNativeReady,null)
04-15 12:22:48.145: D/DroidGap(778): onMessage(onPageFinished,file:///android_asset/www/file_reader.html)
04-15 12:22:48.335: D/CordovaLog(778): in onDeviceReady
04-15 12:22:48.335: I/Web Console(778): in onDeviceReady at file:///android_asset/www/file_reader.html:18
回答1:
Put this line document.addEventListener("deviceready", onDeviceReady, false);
outside of onLoad as the first line in your scripts section.
来源:https://stackoverflow.com/questions/16015471/phonegap-android-native-function-not-getting-called