0

我有以下代码,它将根据请求加载两个不同的 Web 视图。

public class MultipleWebViewHanlder {  
public MultipleWebViewHanlder(Context context) {   
        // TODO Auto-generated constructor stub  
        this.context = context;  
            }  

public void MultipleWebViewfun(final String str) {

final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);  

            final WebView mWebView = new WebView(context);  

            WebSettings settings=mWebView.getSettings(); 
            settings.setJavaScriptEnabled(true);  
            settings.setDatabaseEnabled(true);  
            settings.setDomStorageEnabled(true);  
            settings.setAllowFileAccess(true);  

            settings.setBuiltInZoomControls(true);  
            mWebView.setWebChromeClient(new WebChromeClient());  



             JSInterface.handler.post(new Runnable() {  

                @Override  
                public void run() {  
                    try {  


                        ((Activity)context).addContentView(mWebView, params);  
                                            mWebView.loadUrl(((Activity)context).getResources().getString(R.string.DemoURL));  

                                        } catch (Exception e) {  
                         // TODO Auto-generated catch block  
                         e.printStackTrace();  
                   }
public class HybridActivity extends Activity {  
     public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            WebView webview = new WebView(this);  
            webview.getSettings().setJavaScriptEnabled(true);  
  webview.loadUrl("file:///android_asset/www/html/hyb.html");  
            setContentView(webview);  

        MultipleWebViewHanlder webView2 = new MultipleWebViewHanlder (this);
        webView2.MultipleWebViewfun();

我得到以下异常:

WebView 核心线程异常:

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

我的日志:

04-24 15:32:08.764: WARN/dalvikvm(13212): threadid=8: thread exiting with uncaught exception (group=0x4001d7d0)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212): FATAL EXCEPTION: WebViewCoreThread  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.view.ViewRoot.checkThread(ViewRoot.java:2826)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:616)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:642)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.view.View.invalidate(View.java:5115)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.webkit.WebView.viewInvalidate(WebView.java:2616)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.webkit.WebView.invalidateContentRect(WebView.java:2635)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.webkit.WebView.access$6400(WebView.java:300)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.webkit.WebView$PrivateHandler.handleMessage(WebView.java:8072)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.os.Handler.dispatchMessage(Handler.java:99)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.os.Looper.loop(Looper.java:123)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:691)  
04-24 15:32:08.768: ERROR/AndroidRuntime(13212):     at java.lang.Thread.run(Thread.java:1096)  
04-24 15:32:08.772: WARN/ActivityManager(2490):   Force finishing activity com.Hy5/.activity.Hy5CanvasActivity  
4

2 回答 2

1

也许如果你把这段代码放在里面:

runOnUiThread(new Runnable() {
    public void run() {
        //stuff that updates ui
    }
});

它会起作用的

于 2012-04-24T08:28:42.023 回答
0

请参阅:Android“只有创建视图层次结构的原始线程才能接触其视图。”

您的问题源于这里:

JSInterface.handler.post(new Runnable() { 
    public void run() {  
           try {  
               ((Activity)context).addContentView(mWebView, params); 
               ...

您正在尝试将 webView 添加到主线程之外的上下文中。您应该能够将 UI 逻辑嵌套在代码块中Activity.runOnUiThread

于 2018-02-13T17:18:47.080 回答