简体   繁体   English

科尔多瓦sendJavascript不适用于android processMessage失败:错误:未定义:10

[英]Cordova sendJavascript not working for android processMessage failed: Error: not defined:10

I am building a phonegap-android application and as a part of the flow, I am calling an activity from my javascript side using javascriptInterface 我正在构建一个phonegap-android应用程序,作为流程的一部分,我正在使用javascriptInterface从我的javascript端调用活动

This looks like 看起来像

public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        super.init();
        appView.addJavascriptInterface(this, "Android");

        super.loadUrl(Config.getStartUrl());


}

now I also want to send back some data from my activity to the cordova webview. 现在,我还想将活动中的一些数据发送回Cordova WebView。

I have defined a function in my javascript file which I want to call 我在我的javascript文件中定义了一个要调用的函数

function jsi_getData(data) {
     console.log("JSI GET IMAGE INVOKED ON JAVASCRIPT SIDE");
     alert(data);
 }

and in my android code, inside onActivityResult, I am calling this javascript function 在我的android代码中,在onActivityResult内部,我正在调用此javascript函数

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if(requestCode == 12321){
             //do some work

           Log.i("MAIN_ACTIVITY", "***************  received data"+ data);
            appView.sendJavascript("javascript:jsi_getImage("+data+");");

        }
    }

This gives me the following error 这给我以下错误

09-16 18:18:27.211: I/Web Console(28991): processMessage failed: Message: Jjavascript:jsi_getData('data');:1034
09-16 18:18:27.211: I/Web Console(28991): processMessage failed: Error: ReferenceError: jsi_getImage is not defined:1035
09-16 18:18:27.211: I/Web Console(28991): processMessage failed: Stack: ReferenceError: jsi_getImage is not defined
09-16 18:18:27.211: I/Web Console(28991):     at eval (eval at processMessage (file:///android_asset/www/js/libs/cordova.js:996:26), <anonymous>:1:1)
09-16 18:18:27.211: I/Web Console(28991):     at processMessage (file:///android_asset/www/js/libs/cordova.js:996:13)
09-16 18:18:27.211: I/Web Console(28991):     at Function.androidExec.processMessages (file:///android_asset/www/js/libs/cordova.js:1063:13)
09-16 18:18:27.211: I/Web Console(28991):     at pollOnce (file:///android_asset/www/js/libs/cordova.js:933:17):1036
09-16 18:18:27.481: I/Adreno200-EGLSUB(28991): <ConfigWindowMatch:2087>: Format RGBA_8888.

I also changed a appView.sendJavascript() to this.sendJavascript() and I still have the same error Please help as I am litrally stuck!! 我也将appView.sendJavascript()更改为this.sendJavascript() ,但仍然出现相同的错误。 This would be very helpful. 这将非常有帮助。

Thanks 谢谢

Had a similar issue and I was going MENTAL with it, finally I found came up with an "UGLY" but working solution: 有一个类似的问题,我打算使用它,最后我发现想出了一个“丑陋”但可行的解决方案:

Instead of passing the data back from the onActivityResult do the following. 请执行以下操作,而不是将数据从onActivityResult中传回。

1) Define a global variable in your JAVA and store the result in that global variable 1)在您的JAVA中定义一个全局变量,并将结果存储在该全局变量中

2) DO this in the onActivityResult: 2)在onActivityResult中执行以下操作:

Log.i("MAIN_ACTIVITY", "***************  received data"+ data);
appView.sendJavascript("jsi_getImage();");

3) Define another function in you JAVA something like: 3)在您的JAVA中定义另一个功能,例如:

public String send_picture() {
return previously_defined_global_variable;
}

4) In your Javascript function called jsi_getImage, make another call to send_picture() like: 4)在名为jsi_getImage的Javascript函数中,再次调用send_picture():

function jsi_getImage(){
alert(window.MainActivity.send_picture());
}

Make sure that you are sending only string data between Java and Javascript (BASE64 encode for image) 确保您仅在Java和Javascript之间发送字符串数据(图像的BASE64编码)

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cordova processMessage失败:堆栈:未定义(和)错误:非法访问 - Cordova processMessage failed: Stack: undefined (and) Error: illegal access processMessage失败:Stack:ReferenceError:未定义onNotification - processMessage failed: Stack: ReferenceError: onNotification is not defined 应用程序无限循环,processMessage失败错误 - Application infinite loop with processMessage failed error Cordova “Origin: file://”-错误使用和 Android 10,旧版本工作 - Cordova “Origin: file://”-Error using and Android 10, older versions working 引用错误onnotification未在推送通知cordova android中定义 - Reference error onnotification is not defined in push notification cordova android 如何在 Android 10 上使用 Cordova 创建文件夹? - How to create a folder using Cordova on Android 10? 用户定义的函数在Angular Cordova中不起作用 - user defined function not working in Angular Cordova 科尔多瓦运行android由于android studio失败 - cordova run android failed due to android studio 使用cordova 登录Google 将不起作用。 Android 中的错误 10 和 IOS 中的错误请求 - Google login with cordova won't work. Error 10 in Android and bad request in IOS Typescript cordova接口抛出引用错误“ReferenceError:Cordova未定义” - Typescript cordova interface throws reference error “ReferenceError: Cordova is not defined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM