android中java和JS互相调用,Web Console: Uncaught TypeError: Object [object Object] has no method
来源:互联网 发布:多益网络2016校园招聘 编辑:程序博客网 时间:2024/06/09 23:06
在Android中写Hybird程序,需要JS和Android的方法互相调用,写好之后应用跑起来,发现Android调JS可以,但是JS代码调用Android的代码不行,报错如下:
Web Console: Uncaught TypeError: Object [object Object] has no method...
google一下后发现,4.2的版本前存在重大安全隐患,因为JavaScript 可以使用反射访问注入webview的java对象的public fields,在一个包含不信任内容的WebView中使用这个方法,会允许攻击者去篡改宿主应用程序,使用宿主应用程序的权限执行java代码。因此4.2以后,任何为JS暴露的接口,都需要加
@JavascriptInterface所以解决方法就是:
在你暴露给JS调用的方法前面加上@JavascriptInterface,导入android.webkit.JavascriptInterface这个包就行了。
这个是4.2之前的写法:
public class JSCallAndroid {private Context context;public JSCallAndroid(Context context) {this.context = context;}//暴露给JavaScropt的方法public void testJS(String msg) {AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("在JS中调用Android").setMessage("调用成功").create().show();}}
这是4.2之后的写法
public class JSCallAndroid {private Context context;public JSCallAndroid(Context context) {this.context = context;}//暴露给JavaScript的方法@JavascriptInterfacepublic void testJS(String msg) {AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setTitle("在JS中调用Android").setMessage("调用成功").create().show();}}
0 0
- android中java和JS互相调用,Web Console: Uncaught TypeError: Object [object Object] has no method
- [Android] Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'
- 【Android】Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'
- [Android] Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'
- JS调用Android中的方法时出现 "Uncaught TypeError: Object [object Object] has no method
- js调用java方法的遇到的坑Uncaught TypeError: Object [object Object] has no method
- Android webview js 调用java方法报错"Uncaught TypeError: Object [object Object] has no method xx
- WebView注入Java对象注意事项 [INFO:CONSOLE(475)] "Uncaught TypeError: Object [object Object] has no method
- Android js交互错误:Uncaught TypeError: Object [object Object] has no method 'xxx'
- sencha touch中出现JS错误Uncaught TypeError: Object [object Object] has no method 'getForm'
- Uncaught TypeError: Object [object Object] has no method 'live'
- jquery: Uncaught TypeError: Object [object Object] has no method 'live'
- Uncaught TypeError: Object [object Object] has no method 'xxx'
- Uncaught TypeError: Object [object Object] has no method 'highcharts'
- Uncaught TypeError: Object #<Object> has no method 'addEvents'
- Uncaught TypeError: Object #<Object> has no method 'addEvents'
- 安卓WebView报错:android uncaught typeerror object object object has no method
- ztree “Uncaught TypeError: Object # has no method 'toLowerCase'”
- 亨元模式——池技术
- hadoop的用户代理机制
- 黑马程序员——OC基础03—内存管理
- css3聊天框
- 详解SpringMVC中Controller的方法中参数的工作原理
- android中java和JS互相调用,Web Console: Uncaught TypeError: Object [object Object] has no method
- Objective-C 基础之强大的字面量
- flux护眼软件设置
- JAVA 中BIO,NIO,AIO的理解
- Android微信支付遇到的坑
- 代码狗必看:前端开发3大坑
- Objective-C基础之常用简单设计模式
- php做app接口安全方案
- dwin串口屏指令