在android中没有调用WebView方法

use*_*958 7 methods android webview

我的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;
}
Run Code Online (Sandbox Code Playgroud)

小智 15

正如警告所说,你正在调用webview方法WebViewCoreThread.因此修改你的代码,

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;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)


小智 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();");
Run Code Online (Sandbox Code Playgroud)