Android中的WebView方法未被调用

7

我的Web视图没有调用javascript函数,返回以下警告。有人能建议如何消除下面的警告吗?

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.

以下是我的函数。

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;
}
6个回答

16

正如警告所说,您正在WebViewCoreThread中调用webview方法。 因此,请修改您的代码如下:

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;
        }
    });
}

6
警告提示了所有信息。您直接调用了Webview方法,这意味着您在WebViewCoreThread上调用了它们。您必须在UI线程上调用它们,也就是在使用此webview的Activity中调用它们。
例如:
WebView wv = (WebView)findViewById(id);
wv.setJavaScriptEnabled(true);
wv.setJavaScriptCanOpenWindowsAutomatically(true);
wv.setPluginsEnabled(true);
wv.loadUrl("javascript:android.selection.longTouch();");

如果我在AsyncTask中调用任何WebView方法,那么它是否会出现问题? - David Dimalanta
这是被接受的答案,我看不到WebView实例的setJavaScriptEnabled()或其他这些方法。 - seema
我猜你正在使用更新版本的Android。我想这篇文章是在Android 2.3.3上发布的,但我不确定。在更新的版本中,你需要使用以下代码:wv.getSettings().setJavaScriptEnabled(true)。希望这能解决你的问题。 - ZeusNet

2

使用这段代码,我认为它会对你有用,并根据你的需求进行修改 ##

    private WebView webView;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web);

        webView = (WebView) findViewById(R.id.web_view);
        webView.setInitialScale(1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);
        webView.loadUrl("http://www.youtube.com");

    }

}

好的,如果我想在Webview上运行JavaScript命令怎么办? - Jaxx0rr

0

你可以通过Runnable来使用WebView,无需使用Activity。

    webView.post(new Runnable()
    {
        @Override
        public void run()
        {
          getSettings().setJavaScriptEnabled(true);
          getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
          getSettings().setPluginsEnabled(true);
          loadUrl("javascript:android.selection.longTouch();");
          mScrolling = true;
        }
    });

0

onLongClick是webview的成员吗?

似乎你不能在线程“WebViewCoreThread”上调用所有WebView方法。

你可以使用handler,在onLongClick中向handler发送消息,然后在你的handler中调用WebView方法。


0

我认为你必须在runOnUIThread()中执行onLongClick方法的代码,或者使用Handler,在工作线程上使用所有这些会导致此警告。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接