在线程A的WebView无法创建

在线程A的WebView无法创建

本文介绍了在线程A的WebView无法创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我创造了一些意见和prepare他们要显示某些线程。其中我也有一个web视图。这code在线程中执行:

i have some threads in which i create some views and prepare them to be displayed. Among them i also have a WebView. This code is executed in thread:

WebView lGraphWebView = null;
        try{
            lGraphWebView = new WebView(AppController.getAppController());
        }catch (Exception e) {
            Log.d("info", "error: " +e );
        }

和它抛出了以下异常:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

这是一个有些奇怪,因为当我创建了一个简单的按钮,一切正常。所以,任何人都可以explane我为什么创造一个web视图中我得到这个例外,如果活套。prepare()能有所帮助?在此先感谢!

This is a bit strange, because when i create a simple button all is OK. So, can anyone explane to me why on creation of a WebView i get this exception and if Looper.prepare() can help here?Thanks in advance!

推荐答案

在一般情况下,它不是安全地创建视图主线程之外。

In general, its not safe to create view outside of main thread.

在特定的情况下,这是不允许的,因为的WebView 创建处理程序()在其构造为通信与UI线程。但是,由于处理程序的默认构造函数将其自身附加到当前线程和当前线程不具有尺蠖运行,你'重新获得此异常。

In your particular case, this is not allowed, because WebView creates Handler() in its constructor for communication with UI thread. But since Handler's default constructor attaches itself to current thread, and current thread does not have Looper running, you're getting this exception.

您可能会认为,建立一个弯针线(必须是活着的,至少只要的WebView )可以帮助你,但是这其实是一个危险的路要走。我不会推荐它。

You might think that creating a looper thread (that must be alive at least as long as WebView) might help you, but this actually a risky way to go. And I wouldn't recommend it.

您应该坚持在主线程创建的WebView 秒。所有控制通常用于快速构建优化的,因为它们是在UI线程几乎总是创建。

You should stick with creating WebViews in main thread. All controls are usually optimized for fast construction, as they are almost always created in UI thread.

这篇关于在线程A的WebView无法创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:39