Android加载webview 弹出错误

来源:互联网 发布:2016年最红的网络歌曲 编辑:程序博客网 时间:2024/05/16 14:48

      项目中需要加载本地的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里的网页回答如下:



5down votefavorite

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;}
shareimprove this question
 

5 Answers

activeoldestvotes
up vote6down voteaccepted

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();");
shareimprove this answer
 
 
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 
up vote15down vote

As the warning says you are calling the webview methods in the WebViewCoreThread. 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();}


5down votefavorite

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;}
shareimprove this question
 

5 Answers

activeoldestvotes
up vote6down voteaccepted

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();");
shareimprove this answer
 
   
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 
up vote15down vote

As the warning says you are calling the webview methods in the WebViewCoreThread. 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;        }    });}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机解锁键坏了怎么办 lv包肩带胶熔了怎么办 手机膜进空气了怎么办 铁的下水管漏水怎么办 刚浇的水泥开裂怎么办 马桶移位器堵了怎么办 分散片直接吃了怎么办 坐马桶拉不出来怎么办 泥状面膜干了怎么办 苹果6s掉水里了怎么办 手机充电口松了怎么办 手机液体膜碎了怎么办 车膜贴的太黑了怎么办 苹果7后壳氧化怎么办 苹果6s后盖氧化怎么办 苹果6s后壳氧化怎么办 食道癌吃了就吐怎么办 有眼袋和泪沟怎么办? 泪沟和眼袋都有怎么办 脸上的汗毛很长怎么办 点痣留下褐色印怎么办 氮氧传感器坏了怎么办 考试车离合太松怎么办 胎心监护不过关怎么办 羚羊角的功效与作用发烧怎么办 小孩上课注意力不集中怎么办 2岁宝宝不会说话怎么办 小孩脖子上长淋巴结怎么办 小孩子上课注意力不集中该怎么办 脸过敏干燥起皮怎么办 脸上皮肤干燥起皮怎么办 身上皮肤干燥起皮怎么办 皮肤暗黄有色斑怎么办 我皮肤干燥暗黄怎么办 脸上有皮肤暗黄怎么办 皮肤暗黄毛孔大怎么办 脸上很干燥起皮怎么办 脸上的皮肤起皮怎么办 身体的皮肤好干怎么办 滴油雾化器炸油怎么办 已经发炎的痘痘怎么办