【问题标题】:java.lang.IllegalStateException: Calling View methods on another thread than the UI threadjava.lang.IllegalStateException:在 UI 线程之外的另一个线程上调用 View 方法
【发布时间】:2013-12-13 21:20:00
【问题描述】:

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

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 4.4”是什么意思?你有什么代码要显示吗?
  • 这个异常意味着你应该在 UI 线程中运行你的代码。你可以使用 handler 或 runOnUiThread 来解决这个问题

标签: android multithreading illegalstateexception android-4.4-kitkat webviewchromium


【解决方案1】:

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

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

            // TODO Your code
        }
    });

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2019-07-11
        • 2012-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多