Android加载webview 弹出错误

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



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

5 Answers

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
 
 
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 

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() {
			@Override
			public 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() {
							@Override
							public void run() {
								first_chart_webview
										.loadUrl("javascript:resetMockTicks("
												+ json + ","
												+ hisDataList.size() + ",1,1)");
							}
						});

					}

				} finally {
					Runnable work = new Runnable() {

						@Override
						public void run() {
							getProgressDialog().dismiss();
						}
					};
					getHandler().post(work);
				}
			}
		}.start();

	}


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

5 Answers

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
 
   
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 

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;
        }
    });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值