A WebView in a thread can't be created

时光怂恿深爱的人放手 提交于 2019-11-29 10:56:09

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

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.

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.

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.

You should not create or manipulate views in threads other than the main UI thread. For instance, you can use the Handler to post to the UI thread:

private Handler handler = new Handler();

handler.post(new Runnable() {
   public void run() {
       lGraphWebView = new WebView(AppController.getAppController());
   }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!