2012-04-24 61 views
0

我有以下代码,它将根据请求加载两个不同的Web视图。在android中加载2个单个活动的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 

回答

1

也许如果你把里面的代码如下:

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

,将工作

+0

不......它没有......在这里也抛出相同的异常。 – Smitha 2012-04-24 08:39:43

相关问题