Smitha
Smitha

Reputation: 6134

What is causing this in android webView?

I am getting the following exception and not able to debug it.

Please help.
Code:

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);

Log:

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

Upvotes: 4

Views: 2864

Answers (1)

fardjad
fardjad

Reputation: 20394

It's hard to say without seeing the code, but it seems you're trying to change an element from outside of it's owner thread (this could be happened because you're trying to change UI elements from other threads).

So try this: There's a helper function runOnUiThread() that accepts a runnable as argument:

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

runOnUiThread(runnable);

Upvotes: 5

Related Questions