我有以下代码,它会根据请求加载两个不同的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
答案 0 :(得分:1)
也许你把这个代码放在这个:
runOnUiThread(new Runnable() {
public void run() {
//stuff that updates ui
}
});
它会起作用
答案 1 :(得分:0)
请参阅:Android "Only the original thread that created a view hierarchy can touch its views."
您的问题源于此处:
JSInterface.handler.post(new Runnable() {
public void run() {
try {
((Activity)context).addContentView(mWebView, params);
...
您尝试将webView添加到主线程之外的上下文中。您应该能够在代码块Activity.runOnUiThread