安卓:CalledFromWrongThreadException距离的WebView shouldOverrideUrlLoading [英] Android: CalledFromWrongThreadException from WebView's shouldOverrideUrlLoading

查看:113
本文介绍了安卓:CalledFromWrongThreadException距离的WebView shouldOverrideUrlLoading的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发上以某种方式对三星Galaxy S1得到一个 CalledFromWrongThread 异常崩溃的库(API V7 - 的Andr​​oid 2.1)。在code是这样的:

I am developing on a library that is somehow getting a CalledFromWrongThread Exception crash on Samsung Galaxy S1 (api v7 - android 2.1). The code is something like this:

class MyWebViewClient extends WebViewClient {
    @Override
    public void shouldOverrideUrlLoading(WebView view, String url) {
        someListener.addToUiView();
    }
}

当然,这实际上是引发错误(它实现了一个监听器回调)方法:

and of course, the method that is actually throwing the error (which implements a listener callback):

View v;
public void addToUiView(){
    v.addView(new TextView(context)); //<-- this line is throwing the error on rare occasions
}

我跳过两者之间的某个code,但我没有做任何事情在其他地方怪异。另请注意:此崩溃只似乎已经发生的时间非常非常小的%。 (不一定是决定性的,因为不是每个人都报告他们的数据)。

I'm skipping some code in between, but i'm not doing anything weird in other places. Also note: this crash only seems to have been happening a very very small % of the time. (not necessarily conclusive, as not everyone reports their data).

有其他人碰到这种?是WebCore的线程弄乱的东西了?

has anyone else come across this?? Is WebCore threading messing things up?

推荐答案

现在我还没有实际测试过这一点,但我要回答的我所知。这就是说,我的直觉告诉我,你只看到的错误间歇性,因为从的WebView Web请求(浏览器)与异步性的不同程度的发生,可能是使用一个线程池做到这一点。基本上,有时它请求资源并联,有时它没有。更糟糕的是,你可能只有一个单一的设备上看到此错误,因为OEM厂商根据自己的preferences和意见优化操作系统级code(如的WebView 内部) (特别是三星)。无论哪种方式,真正的问题是,你是在肯定不能保证是UI友好......也就是说,任何地方你得到一个子系统回调的地方做一些相关的UI。

Now I haven't actually tested this but I am going to answer to the best of my knowledge. That said, my instinct is telling me that you are only seeing the error intermittently because web requests from a WebView (browser) happen with varying levels of asynchronicity and probably utilize a thread pool to accomplish this. Basically, sometimes it requests resources in parallel and sometimes it doesn't. Worse yet you might be seeing this error on only a single device because OEMs optimize OS level code (like the WebView internals) based on their preferences and opinions (especially Samsung). Either way the real problem is that you are doing something "UI related" in a place that is definitely not guaranteed to be "UI friendly"... That is, anywhere where you are getting a subsystem callback.

你的问题的解决方案比解释更简单:只需使用背景(即我假设是活动)..

The solution to your problem is much more simpler than the explanation: just use your context (that I am assuming is an Activity)..

活动活动的一个内置的函数调用<一个href=\"http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29\"相对=nofollow> runOnUiThread(Runnable接口) 将守卫可运行里面的code从错误的线程上运行。基本上,你的问题是非常常见的和Android有一个内置的解决方案。 runOnUiThread 如果需要的话,只会增加开销,换句话说,如果你的线程是UI线程,它只是运行的Runnable ,如果它是不是使用了正确的处理程序(与UI线程关联的一种)运行的Runnable

Activitys have a built in function called runOnUiThread(Runnable) that will guard the code inside the runnable from running on the wrong thread. Basically, your problem is really common and android has a built-in solution. runOnUiThread will only add overhead if required, in other words if your thread is the UI thread, it will just run the Runnable, if it isn't it uses the correct Handler (the one associated with the UI thread) to run the Runnable.

下面是它看起来应该是这样的:

Here is what it should look like:

View v;
public void addToUiView() {

    final Activity activity = (Activity) context;

    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {

            v.addView(new TextView(activity));
        }

    });
}

I codeD,截至权利的SO窗口内,所以我的任何令人震惊的错误表示歉意,我希望帮助,让我知道,如果你需要更多的信息或这不能解决您的问题-ck

i coded that up right inside the SO window so my apologies for any egregious errors, I hope that helps, and let me know if you need more info or of this doesn't solve your problem -ck

这篇关于安卓:CalledFromWrongThreadException距离的WebView shouldOverrideUrlLoading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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