java.lang.IllegalStateException:在除UI线程之外的另一个线程上调用View方法

问题描述 投票:5回答:3

当我尝试使用Android 4.4(Kit kat)在我的nexus 4上打开webview时,我会收到以下错误消息:

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)

自从我更新到Android 4.4我的Nexus 4。

android multithreading illegalstateexception android-4.4-kitkat webviewchromium
3个回答
6
投票

只需检查4.4谷歌的迁移网页视图添加并更改了一些内容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);
}

7
投票

你的代码是什么样的?你可以试试

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

            // TODO Your code
        }
    });

0
投票

如果runOnUiThread由于某种原因不可用,您还可以使用以下代码来绕过此错误(假设您想要访问View而不需要显示它):

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.