7

When trying to open a webview on my nexus 4 now with Android 4.4 (Kit kat) I'll getting this error message:

Calling View methods on another thread than the UI thread.; 
java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.
com.android.webview.chromium.WebViewChromium.createThreadException(WebViewChromium.java:268)

Since i update to Android 4.4 my Nexus 4.

Charles
  • 50,943
  • 13
  • 104
  • 142
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    Could you please clarify? How exactly are you opening the web view? What do you mean by "Since I update to Android 4.4"? Do you have any code to show? – EJK Nov 28 '13 at 01:02
  • 1
    the exception means you should run your code in UI thread. you can use handler or runOnUiThread to fix this issue – yushulx Nov 28 '13 at 03:31

3 Answers3

8

what's your code like? you can try

 runOnUiThread(new Runnable() {
        @Override
        public void run() {

            // TODO Your code
        }
    });
Jerome
  • 1,749
  • 1
  • 14
  • 40
6

just check migrating web view of 4.4 google added and changed some things in it here

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // Code for WebView goes here
    }
});


// This code is BAD and will block the UI thread
webView.loadUrl("javascript:fn()");
while(result == null) {
  Thread.sleep(100);
}
MattC
  • 12,285
  • 10
  • 54
  • 78
pavanmvn
  • 749
  • 10
  • 21
1

If runOnUiThread is not available for whatever reason, you can also use the following code to bypass this error (say you are wanting access to a View without needing to display it):

Handler handler = new Handler(Looper.getMainLooper());
try
{
    handler.post(
        new Runnable()
        {
            @Override
            public void run()
            {
                DesiredMethod(); // Where this method runs the code you're needing
            }
        }
    );
} catch (Exception e)
{
    e.printStackTrace();
}
Graeme Campbell
  • 364
  • 2
  • 4