如何WebView.getTitle()从UI线程? [英] How to WebView.getTitle() from a UI thread?

查看:449
本文介绍了如何WebView.getTitle()从UI线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的WebView 的子类,我曾经有过这一行的getTitle()的覆盖方法:

In a subclass of WebView, I used to have this line in an overridden method of getTitle():

String title = super.getTitle();

这在所有版本的机器人,直到我来测试我的应用程序上的的Andr​​oid 4.1 手机,它给了我这个警告<$ C $运行良好C> super.getTitle()行:

It worked well in all versions of Android, until I got to test my app on an Android 4.1 phone, which gave me this warning on that super.getTitle() line:

12-20 21:38:27.467:W / webview_proxy(2537):java.lang.Throwable中:   警告:的WebView方法被调用的线程的WebViewCoreThread。   所有的WebView方法必须调用UI线程。web视图的未来版本可能不支持在其他线程使用。

12-20 21:38:27.467: W/webview_proxy(2537): java.lang.Throwable: Warning: A WebView method was called on thread 'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

所以,我通过 runOnUiThread通过它()想解决这个新法令的工作

Activity a = this.getActivity();
a.runOnUiThread(new Runnable() {
    public void run() { 
    String title = super.getTitle();
    }
});

但是,这code甚至不会编译,因为不再引用的WebView ,而是以活动

But this code won't even compile because super no longer refers to WebView, but rather to Activity.

不知道如何如何 super.getTitle()从UI线程? (与上述,在限制的getTitle() 的WebView的一个子类

Any idea how How to super.getTitle() from the UI thread? (with the constraints described above, in the getTitle() of a subclass of WebView)

推荐答案

摩根的回答,虽然它可能解决编译错误,是不是一个真正的解决这个问题。

Morgan's answer, while it may fix the compile error, is not really a solution to this problem.

首先,它无助于改变调用的getTitle()来一个不同的线程。这个根本的问题就是为什么Android正在给你在运行时的错误。

First of all, it does nothing to change the call to getTitle() to a different thread. That underlying issue is why Android is giving you the error at runtime.

您说在注释

情节,我称其为WebViewClient.onPageFinished(),这恰好不是要在UI线程上。

The circumstances are that I am calling it in WebViewClient.onPageFinished(), which happens not to be on the UI thread.

这的可以的是一个问题。如果你开始从UI线程的Web请求,那么 onPageFinished()当然应该被调用回UI线程。你能解释一下你是如何开始的Web请求,而为什么的你正在做的呀?的大多数时间,你不应该看到 onPageFinished()在后台调用,所以你可能有一个问题在其他地方。

That may be a problem. If you are starting the web request from the UI thread, then onPageFinished() should certainly get called back on the UI thread. Can you explain how you are starting the web request, and why you're doing it that way? The vast majority of the time, you shouldn't be seeing onPageFinished() called in the background, so you may have a problem elsewhere.

(注意:如果你认为你需要调用 WebView.loadUrl()的背景下,以避免阻塞UI,请的see~~V在这个问题上这等答案)

(Note: if you think you need to call WebView.loadUrl() in the background to avoid blocking the UI, please see this other answer on that issue)

如果你真的认为你需要开始在后台的Web请求,你可以看到 onPageFinished()在后台调用,你需要注意调用的getTitle()在UI线程上。

If you really think you need to start the web request in the background, and you see onPageFinished() called in the background, you need to take care to call getTitle() on the UI thread.

另外,如果你是从 onPageFinished()方法调用它,那么就没有必要用这样的语法:

Also, if you are calling it from the onPageFinished() method, then there is no need to use syntax like this:

String title = MyWebView.this.getTitle();

在该方法中,你是通过你的Web视图的实例,因此只需直接使用它:

in that method, you are passed the instance of your web view, so just use it directly:

public void onPageFinished (WebView view, String url) {
    String title = view.getTitle();
}

不过,正如我所说的,这并没有解决线程问题。您需要向我们展示的为什么您要使用页面标题的那个方法,但是的单向的安全使用它会是这样的:

but, as I said, that doesn't address the threading issue. You would need to show us why you are trying to use the page title in that method, but one way to safely use it would be something like this:

public void onPageFinished (final WebView view, String url) {
   view.post(new Runnable() {
      public void run() { 
         String title = view.getTitle();
         // do something with title that affects the UI here
      }
   });     
}

注意,我需要做的视图参数最后在上面$ C $℃。

Note that I needed to make the view parameter final in the above code.

这篇关于如何WebView.getTitle()从UI线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆