项目中需要加载本地的json数据放到webview上显示,之前加载url的数据,都是新建一个线程然后加载图形,不过突然出现如下错误:

07-30 10:15:44.031: W/webview_proxy(3770): java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.


错误很明显是说webview的进程需要放到主线程里面来做,引入stack里的网页回答如下:



5 down vote favorite

My Web view is not calling the javascript function it is returning warning like below. Can anybody suggest how to get rid of the below warning.

07-30 10:15:44.031: W/webview_proxy(3770): java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

Below is my function.

public boolean onLongClick(View v){    System.out.println("dfdsf");    // Tell the javascript to handle this if not in selection mode    //if(!this.isInSelectionMode()){        this.getSettings().setJavaScriptEnabled(true);        this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);        this.getSettings().setPluginsEnabled(true);        this.loadUrl("javascript:android.selection.longTouch();");        mScrolling = true;        //this.setJavaScriptEnabled(true);    //}    // Don't let the webview handle it    return true;}
android methods webview
share improve this question edited Jul 30 '13 at 10:25 Chintan Rathod 12.2k 7 43 67 asked Jul 30 '13 at 10:19 user1048958 156 1 4 15
add a comment

5 Answers

active oldest votes
up vote 6 down vote accepted

The warning is telling you everything. You are calling the webview methods directly. That means you are calling them on WebViewCoreThread. You have to call them on the UI Thread that means in the Activity which uses this webview.

Like:

WebView wv = (WebView)findViewById(id);wv.setJavaScriptEnabled(true);wv.setJavaScriptCanOpenWindowsAutomatically(true);wv.setPluginsEnabled(true);wv.loadUrl("javascript:android.selection.longTouch();");
share improve this answer answered Jul 30 '13 at 10:26 ZeusNet 484 5 14
Is it same thing in the problem that if I called any WebView methods in AsyncTask, then it will not work?– David Dimalanta Oct 3 '13 at 7:38
This is accepted answer and I cant see setJavaScriptEnabled() or other these methods for WebView instance.– seema Aug 20 '15 at 12:10
I guess you are on a newer version of Android. This post was published on Android 2.3.3 i think. I'm not sure at all. In the newer versions you have to use the following code:wv.getSettings().setJavaScriptEnabled(true). Hope this solves your problem– ZeusNet Aug 24 '15 at 8:47
add a comment
up vote 15 down vote

As the warning says you are calling the webview methods in theWebViewCoreThread. Thus modify your code like this,

public boolean onLongClick(View v){    YourActivity.this.runOnUiThread(new Runnable() {        public void run() {            this.getSettings().setJavaScriptEnabled(true);            this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);            this.getSettings().setPluginsEnabled(true);            this.loadUrl("javascript:android.selection.longTouch();");            mScrolling = true;        }    });}
我采用的第二个解决方式,runOnUiThread方法,我的代码如下:

// 加载历史数据到图形界面public void loadHisData(final int cycle, final String instname) {getProgressDialog().show();new Thread() {@Overridepublic void run() {try {hisDataList = TradeAPI.getInstance().queryHisData(StaticContext.CHART_INST,StaticContext.CHART_CYCLE_60MIN);if (hisDataList != null) {final String json = JSONUtil.getJSON4HistoryData(hisDataList);activity.runOnUiThread(new Runnable() {@Overridepublic void run() {first_chart_webview.loadUrl("javascript:resetMockTicks("+ json + ","+ hisDataList.size() + ",1,1)");}});}} finally {Runnable work = new Runnable() {@Overridepublic void run() {getProgressDialog().dismiss();}};getHandler().post(work);}}}.start();}


5 down vote favorite

My Web view is not calling the javascript function it is returning warning like below. Can anybody suggest how to get rid of the below warning.

07-30 10:15:44.031: W/webview_proxy(3770): java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

Below is my function.

public boolean onLongClick(View v){    System.out.println("dfdsf");    // Tell the javascript to handle this if not in selection mode    //if(!this.isInSelectionMode()){        this.getSettings().setJavaScriptEnabled(true);        this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);        this.getSettings().setPluginsEnabled(true);        this.loadUrl("javascript:android.selection.longTouch();");        mScrolling = true;        //this.setJavaScriptEnabled(true);    //}    // Don't let the webview handle it    return true;}
android methods webview
share improve this question edited Jul 30 '13 at 10:25 Chintan Rathod 12.2k 7 43 67 asked Jul 30 '13 at 10:19 user1048958 156 1 4 15
add a comment

5 Answers

active oldest votes
up vote 6 down vote accepted

The warning is telling you everything. You are calling the webview methods directly. That means you are calling them on WebViewCoreThread. You have to call them on the UI Thread that means in the Activity which uses this webview.

Like:

WebView wv = (WebView)findViewById(id);wv.setJavaScriptEnabled(true);wv.setJavaScriptCanOpenWindowsAutomatically(true);wv.setPluginsEnabled(true);wv.loadUrl("javascript:android.selection.longTouch();");
share improve this answer answered Jul 30 '13 at 10:26 ZeusNet 484 5 14
Is it same thing in the problem that if I called any WebView methods in AsyncTask, then it will not work?– David Dimalanta Oct 3 '13 at 7:38
This is accepted answer and I cant see setJavaScriptEnabled() or other these methods for WebView instance.– seema Aug 20 '15 at 12:10
I guess you are on a newer version of Android. This post was published on Android 2.3.3 i think. I'm not sure at all. In the newer versions you have to use the following code:wv.getSettings().setJavaScriptEnabled(true). Hope this solves your problem– ZeusNet Aug 24 '15 at 8:47
add a comment
up vote 15 down vote

As the warning says you are calling the webview methods in theWebViewCoreThread. Thus modify your code like this,

public boolean onLongClick(View v){    YourActivity.this.runOnUiThread(new Runnable() {        public void run() {            this.getSettings().setJavaScriptEnabled(true);            this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);            this.getSettings().setPluginsEnabled(true);            this.loadUrl("javascript:android.selection.longTouch();");            mScrolling = true;        }    });}

更多相关文章

  1. “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
  2. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  3. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  4. Android(安卓)使用WebView 加载新闻详情,添加点击图片的js(使用的
  5. Android之奔溃提示com.google.gson.internal.LinkedTreeMap cann
  6. Android支付宝快捷登录
  7. Android(安卓)ContentObserver实现数据库监听
  8. android抓取网络数据包工具
  9. 实现android按键震动按键声音的机制

随机推荐

  1. JS基础知识:JS对象模拟数组方法用法实例分
  2. Java如何实现登录token令牌
  3. 使用MybatisPlus自定义模版中能获取到的
  4. Android(安卓)相机实例(一)
  5. Android源码目录结构详解
  6. Android(安卓)Volley.jar包下载
  7. Ubuntu10.04 32位编译Android(安卓)4.0源
  8. JS判断当前环境为微信,手机判断浏览器类型
  9. Flutter知识点:数据存储之File
  10. 【小超_U3D】Unity打出Android包,运行报