[英]sendJavascript function not defined error in Cordova Plugin salesforce
I am trying to create a salesforce plugin in which i want to push data back to JS from my java code (which is basically a background process) But its always giving me error for line this.sendJavascript(function name) 我正在尝试创建一个salesforce插件,其中我想将数据从我的Java代码(基本上是一个后台进程)推回JS,但它总是给我这行错误。sendJavascript(函数名)
following is plugin code - 以下是插件代码-
package com.salesforce.androidsdk.phonegap;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* This class echoes a string called from JavaScript.
*/
public class Echo extends CordovaPlugin {
private static final String TAG = "CordovaPlugin";
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
Log.i(TAG,"inside execute method--->>>" + action + args + callbackContext);
if (action.trim().equalsIgnoreCase("echo")) {
Log.i(TAG,"args.getString(0)--->>>" + args.getString(0));
String message = args.getString(0);
// Initialise the service variables and start it it up
Context thiscontext = this.cordova.getActivity().getApplicationContext();
Intent callBackgroundService = new Intent(thiscontext, CallBackgroundService.class);
callBackgroundService.putExtra("loadinterval", 800); // Set LED flash interval
thiscontext.startService(callBackgroundService);
this.echo(message, callbackContext);
sendValue("Kaushik", "Ray");
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.");
}
}
public void sendValue(String value1, String value2) {
JSONObject data = new JSONObject();
try {
data.put("value1", "Kaushik");
data.put("value2", "Ray");
} catch (JSONException e) {
Log.e("CommTest", e.getMessage());
}
String js = String.format(
"window.plugins.commtest.updateValues('%s');",
data.toString());
error line ------->>> this.sendJavascript(js);
}
}
I have marked the error line which is at the end. 我已经在最后标记了错误行。 Please help me resolve this
请帮我解决这个问题
Thanks in Advance, Kaushik 在此先感谢,Kaushik
sendJavascript()
is a function in both DroidGap.java and CordovaWebView.java, but the value of this
in your current file is your instance of Echo. sendJavascript()
是DroidGap.java和CordovaWebView.java中的一个函数,但是在当前文件中, this
值是Echo的实例。 The line this.sendJavascript()
fails because is expecting the function called to be a member of Echo or a public/protected member of one of the classes it inherited from, in this case CordovaPlugin. 这行
this.sendJavascript()
失败,因为期望被调用的函数是Echo的成员,或者是它继承自其中一个类的公共/受保护成员,在本例中为CordovaPlugin。
There is a public variable in CordovaPlugin.java, named webView
which is the CordovaWebView for your project. CordovaPlugin.java中有一个名为
webView
的公共变量,它是您项目的CordovaWebView。 If you change your offending line from this.sendJavascript(js)
to webView.sendJavascript(js)
, it should fix your error. 如果将有问题的行从
this.sendJavascript(js)
更改为this.sendJavascript(js)
webView.sendJavascript(js)
,它应该可以解决您的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.