2013-07-30 43 views
7

中調用我的Web視圖不調用它正在返回警告的javascript函數,如下所示。任何人都可以建議如何擺脫下面的警告。WebView方法未在android

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

警告告訴你一切。您正在直接調用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();"); 
+0

問題中的問題是否如果我在AsyncTask中調用任何WebView方法,那麼它將不起作用? –

+0

這是接受的答案,我不能看到setJavaScriptEnabled()或其他WebView實例的這些方法。 – seema

+0

我想你是在更新版本的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"); 

    } 

} 
0

onLongClick是網頁視圖中的一員呢?

看來你不能調用線程'WebViewCoreThread'上的所有WebView方法。

您可以使用處理程序,將消息發送到onLongClick中的處理程序,然後在處理程序中調用WebView方法。

0

我認爲你必須在runOnUIThread()中執行onLongClick方法的代碼或使用Handler,這個警告是由於在工作線程上使用了所有這些。

15

由於警告說你在調用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; 
     } 
    }); 
} 
0

你可以通過Runnable使用WebView。無需使用活動。

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