WebView在第二次初始化后显示为纯白框 [英] WebView appears as plain white box after the second time it's been initialised

查看:154
本文介绍了WebView在第二次初始化后显示为纯白框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr:即使我似乎正确设置了WebView,WebView还是显示为白框,并且确实前两次都可以运行,但是随后失败了

视频显示了实际存在的问题...

我有以下代码从定义它的xml扩展视图(其中包含WebView):

private void createCard(ViewGroup cvFrame, Card card) {

    //... setup vairables...

    cvFrame.clearDisappearingChildren();
    cvFrame.clearAnimation();


    try {
        View cv = LayoutInflater.from(getBaseContext()).inflate(R.layout.card_back_view,
                cvFrame, true);
        cv.setBackgroundDrawable(Drawable.createFromStream(mngr.open(deckName + "_Card_back.png"), deckName));

        TextView suit = (TextView)cv.findViewWithTag("card_back_suit");
        //...setup text view for suit, this code works fine every time...

        WebView title = (WebView)cv.findViewWithTag("card_back_title");
        //This WebView doesn't appear to be the one which actually appears on screen (I can change settings till I'm blue in the face, with no effect)
        if (title != null) {
            title.setBackgroundColor(0x00000000);
            title.loadData(titleText, "text/html", "UTF-8");
        } else {
            Log.e("CardView", "Error can't find title WebView");
        }
    } catch (IOException e) {
        Log.e("CardView", "Error making cards: ", e);
    }
}

在我的活动"中将此方法作为onCreate方法的一部分调用时,WebView包含正确的代码,并且具有适当的透明性.

我有一个手势侦听器,该手势侦听器将ViewGroup的内容替换为不同的内容(它使顶部的卡片朝左移动,用2号卡片替换顶部的卡片内容,放回顶部的卡片,然后替换2号卡片卡3)

//Gesture listener event
ViewGroup cvFrame = (ViewGroup)findViewById(R.id.firstCard);
cardLoc++
cvFrame.startAnimation(slideLeft);

(onAnimationEnd代码)

public void onAnimationEnd(Animation animation) {
    if (animation == slideLeft) {
        ViewGroup cvFrameOldFront = (ViewGroup)findViewById(R.id.firstCard);
        ViewGroup cvFrameNewFront = (ViewGroup)findViewById(R.id.secondCard);

        createCard(cvFrameOldFront, cards.get((cardLoc)%cards.size()));
        createCard(cvFrameNewFront, cards.get((cardLoc+1)%cards.size()));

        TranslateAnimation slideBack = new TranslateAnimation(0,0,0,0);
        slideBack.setDuration(1);
        slideBack.setFillAfter(true);
        cvFrameOldFront.startAnimation(slideBack);

    } 
}

发生动画并且替换卡中的内容时,TextView suit被很好地替换,并且代码肯定通过了代码以替换WebView内容,但是由于某种原因,我最终得到了一个白色矩形WebView的大小和形状,没有内容,没有透明度.

如果我将WebView更改为TextView,它的内容将被替换掉,所以只有WebView控件:S

会出现此问题

谁能告诉我为什么/建议修复?

解决方案

事实证明,使用LayoutInflater替换ViewGroup的内容时,WebView不会被清除.其他控件似乎都已删除(或者至少findViewWithTag()返回了每个其他控件的正确引用).我刚刚在LayoutInflater填入内容之前添加了cvFrame.removeAllViews()行,从而解决了该问题. 如果有人对此有更好的解释,我会以直截了当的方式提出要点,否则他们只会陷入以太...

EDIT: tl;dr: WebView appears as white box, even though I appear to be setting it up correctly, and indeed it does work the first two times, but fails subsequently)

EDIT: Video showing the problem in action...

I have the following bit of code which inflates a view (Which contains a WebView) from the xml which defines it:

private void createCard(ViewGroup cvFrame, Card card) {

    //... setup vairables...

    cvFrame.clearDisappearingChildren();
    cvFrame.clearAnimation();


    try {
        View cv = LayoutInflater.from(getBaseContext()).inflate(R.layout.card_back_view,
                cvFrame, true);
        cv.setBackgroundDrawable(Drawable.createFromStream(mngr.open(deckName + "_Card_back.png"), deckName));

        TextView suit = (TextView)cv.findViewWithTag("card_back_suit");
        //...setup text view for suit, this code works fine every time...

        WebView title = (WebView)cv.findViewWithTag("card_back_title");
        //This WebView doesn't appear to be the one which actually appears on screen (I can change settings till I'm blue in the face, with no effect)
        if (title != null) {
            title.setBackgroundColor(0x00000000);
            title.loadData(titleText, "text/html", "UTF-8");
        } else {
            Log.e("CardView", "Error can't find title WebView");
        }
    } catch (IOException e) {
        Log.e("CardView", "Error making cards: ", e);
    }
}

When this method is called as part of the onCreate method in my Activity, the WebView contains the correct code, and is suitably transparent.

I have a gesture listener which replaces the contents of the ViewGroup with different content (It animates the top card off to the left, replaces the contents of the top card with card 2, puts the top card back, then replaces card 2 with card 3)

//Gesture listener event
ViewGroup cvFrame = (ViewGroup)findViewById(R.id.firstCard);
cardLoc++
cvFrame.startAnimation(slideLeft);

(onAnimationEnd code)

public void onAnimationEnd(Animation animation) {
    if (animation == slideLeft) {
        ViewGroup cvFrameOldFront = (ViewGroup)findViewById(R.id.firstCard);
        ViewGroup cvFrameNewFront = (ViewGroup)findViewById(R.id.secondCard);

        createCard(cvFrameOldFront, cards.get((cardLoc)%cards.size()));
        createCard(cvFrameNewFront, cards.get((cardLoc+1)%cards.size()));

        TranslateAnimation slideBack = new TranslateAnimation(0,0,0,0);
        slideBack.setDuration(1);
        slideBack.setFillAfter(true);
        cvFrameOldFront.startAnimation(slideBack);

    } 
}

When the animation has happened and I replace the contents of the cards, the TextView suit is replaced fine and the code definitely passes through the code to replace the WebView contents, but for some reason I end up with a white rectangle the size and shape of the WebView, no content, no transparency.

If I change the WebView to a TextView, it's contents is replaced fine, so it's an issue that occurs only with the WebView control :S

Can anyone tell me why / suggest a fix?

解决方案

It turns out the WebView doesn't get cleared down when using the LayoutInflater to replace the contents of a ViewGroup. The other controls all seem to get removed (or at least the findViewWithTag() returns the right reference for every other control). I've just added in the line cvFrame.removeAllViews() immediately before the LayoutInflater does it's stuff and that fixed the issue. If anyone has any better explanation for this I'll throw the points their way otherwise they will just go into the ether...

这篇关于WebView在第二次初始化后显示为纯白框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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