开发者

Android bug: WebViewCore fails with Assertion error

Unfortunately I canno开发者_如何学Got reliably reproduce this error but infrequently I get it and occasionally it gets reported in the live crash logs also. Here's a stacktrace reported by user with Droid 2.2.2 FRG83G

junit.framework.AssertionFailedError
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertNull(Assert.java:233)
at junit.framework.Assert.assertNull(Assert.java:226)
at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:594)
at java.lang.Thread.run(Thread.java:1096)

This seems to be due to this line in WebViewCore.java

Assert.assertNull(sWebCoreHandler);

Somehow sWebCoreHandler which is private static instance of android.os.Handler is not (Thanks @Idolon for the correction) already initialized but I have no clue how to work around or prevent this issue.

This occurs often enough for me to worry. What is also interesting is seemingly happens when the app is loading Activity that doesn't even have WebView though one of the activities does have it.

P.S. This was filed as bug #16258


Looking at the incriminating source code...

public WebViewCore(Context context, WebView w, CallbackProxy proxy,
        Map<String, Object> javascriptInterfaces) {

    //....

    // We need to wait for the initial thread creation before sending
    // a message to the WebCore thread.
    // XXX: This is the only time the UI thread will wait for the WebCore
    // thread!
    synchronized (WebViewCore.class) {
        if (sWebCoreHandler == null) {
            // Create a global thread and start it.
            Thread t = new Thread(new WebCoreThread());

            //...

        }
    }

//...

private static Handler sWebCoreHandler;
// Class for providing Handler creation inside the WebCore thread.
private static class WebCoreThread implements Runnable {
    public void run() {
        Looper.prepare();
        Assert.assertNull(sWebCoreHandler); // this assertion fails
        synchronized (WebViewCore.class) {
            sWebCoreHandler = new Handler() {
        //...

This is executed in the constructor of any WebView and the assertion error comes from the WebCoreThread constructor, which is only called when the sWebCoreHandler is null, so this assertion should never fail... in theory. Except it's ran outside of the synchronized clause and inside a new Thread that is created and started inside a synchronized clause, supposedly only once.

It seems your error is tied to concurrent creation of webviews. If your application has only one activity with one webview, make sure that this activity is not called more often than necessary (=one at a time), that the WebView is created in the onCreate method rather than the activity constructor, that the startActivity is called in the main thread, and you should be good.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜