4

我收到以下异常并且无法调试它。

请帮忙。
代码:

public class HybridActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView webview=(WebView)findViewById(R.id.webkitWebView1);
        WebSettings settings=webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setBuiltInZoomControls(true);
        webview.setWebChromeClient(new WebChromeClient());

        //  webview.loadUrl("file:///android_asset/www/html/hyb.html");
        // webview.loadUrl("file:///android_asset/index.html");
        try {
            webview.loadUrl("http://www.google.com");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        MultipleWebViewHanlder webView2 = new MultipleWebViewHanlder(this);
        webView2.loadNewWebView();
    }
}

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

    public void MultipleWebViewArea(final String command) {

        final RelativeLayout.LayoutParams params = new  RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
        try {
            Log.i("","Inside tryyyyyyyyyyyyyyyyyyyyy");
            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 {
                        Log.i("","Inside tryyyyyyyyyyyyyyyyyyyy b4 add content viewwwwwwwwww");
                        Runnable runnable = new Runnable() {
                            public void run() {
                                ((Activity)context).addContentView(mWebView,params);
                                Log.i("","Inside tryyyyyyyyyyyyyyyyyyyy after add content viewwwwwwwwww ");
                                mWebView.loadUrl(((Activity)context).getResources().getString(R.string.DemoURL));
                            }
                        };
                        ((Activity)context).runOnUiThread(runnable);

日志:

04-20 10:55:58.052: WARN/dalvikvm(9255): threadid=8: thread exiting with uncaught exception (group=0x400207d8)
04-20 10:55:58.092: ERROR/AndroidRuntime(9255): FATAL EXCEPTION: WebViewCoreThread  
04-20 10:55:58.092: ERROR/AndroidRuntime(9255): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.  
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.view.ViewRoot.checkThread(ViewRoot.java:2812)  
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)  
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)  
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.view.View.invalidate(View.java:5115)
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.webkit.WebView.viewInvalidate(WebView.java:2565)
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.webkit.WebView.invalidateContentRect(WebView.java:2584)
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.webkit.WebView.access$6200(WebView.java:304)
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.webkit.WebView$PrivateHandler.handleMessage(WebView.java:7860)
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.os.Looper.loop(Looper.java:123)
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:637)
04-20 10:55:58.092: ERROR/AndroidRuntime(9255):     at java.lang.Thread.run(Thread.java:1096)
04-20 10:55:58.102: WARN/ActivityManager(172):   Force finishing activity com.Hy5/.activity.Hy5CanvasActivity
4

1 回答 1

5

没有看到代码很难说,但您似乎正在尝试从其所有者线程之外更改元素(这可能会发生,因为您正在尝试从其他线程更改 UI 元素)。

所以试试这个:有一个辅助函数runOnUiThread()接受一个可运行的作为参数:

Runnable runnable = new Runnable() {
    public void run() {
        // your code here
    }
}

runOnUiThread(runnable);
于 2012-04-20T05:39:49.663 回答