android webview不会在片段中加载 [英] android webview do not load in fragment

查看:95
本文介绍了android webview不会在片段中加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临着一个非常非常奇怪的问题:我试图在一个片段中加载Webview,但是我想我找不到使它运行的方法,并且我对原因有绝对的了解.奇怪的是,如果在一个Activity中运行,相同的代码也可以工作

I'm facing with a very very strange problem: I'm trying to load a webview in a fragment, but I sware I can't find a way to make it working and I have absolutely any idea about the reason. The strange is that the same identical code works if run in an Activity

这是我非常基本的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <WebView
        android:id="@+id/webview"
        android:background="@color/black"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

这是代码有效的活动

public class ProvaWebview extends Activity {
    WebView wv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prova_webview);

         wv=(WebView)findViewById(R.id.webview);
        WebSettings settings=wv.getSettings();
        wv.setWebChromeClient(new WebChromeClient() {
        });
        final String mimeType = "text/html";
        final String encoding = "UTF-8";
        String html = getHTML();
        settings.setJavaScriptEnabled(true);
        wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
       wv.clearHistory();
       wv.clearCache(true);
       wv.loadUrl("about:blank");
       wv.pauseTimers(); //new code
       wv = null;
    }

    public String getHTML() {
        String html = "<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/hEFqVV5q44E?rel=0&amp;controls=0&amp;showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>";
        return html;
    }



}

这是代码不起作用的片段

and this is the fragment where the code don't work

public class FrTabDescriptionWebView extends Fragment {
    public static final String TAG = FrTabDescriptionWebView.class.getName();


    private WebView wv;


    public static FrTabDescriptionWebView newInstance() {
        FrTabDescriptionWebView fragment = new FrTabDescriptionWebView();

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    public FrTabDescriptionWebView() {
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.activity_prova_webview, null);

    }




    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        wv = (WebView) view.findViewById(R.id.webview);
        WebSettings settings = wv.getSettings();
        wv.setWebChromeClient(new WebChromeClient() {
        });
        final String mimeType = "text/html";
        final String encoding = "UTF-8";
        String html = getHTML();
        settings.setJavaScriptEnabled(true);
        wv.loadDataWithBaseURL("", html, mimeType, encoding, "");

    }

    public String getHTML() {
        String html = "<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/hEFqVV5q44E?rel=0&amp;controls=0&amp;showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>";
        return html;
    }


}

我到底在做什么错了?

我还尝试使用WebViewFragment用getWebView替换wv,但问题仍然存在

I also tryied to use a WebViewFragment replacing wv with getWebView but the problem still remain

谢谢您的帮助

推荐答案

我发现了问题:

我还有另一个活动,该活动显示了嵌入youtube视频的WebView,并使其在关闭该活动时停止,而我正在使用finish方法中的这段代码

I had another Activity showing a WebView embedding a youtube video, and to make it stopping when closing the Activity I was using this code in the finish method

       wv.clearHistory();
       wv.clearCache(true);
       wv.loadUrl("about:blank");
       wv.pauseTimers();
       wv = null;

避免使用此代码使该片段正常工作.我认为导致问题的真正代码是

avoiding this code made the fragment working. I think that the real code causing the problem was

 wv.pauseTimers();

确认

经过一些测试,我可以确认真正的问题是

After some testing I can confirm that the real problem was

wv.pauseTimers();

删除该行,一切正常,使youtube视频停止播放此代码

removing this line everything works as expected, to make a youtube video stop playing this code works fine

wv.clearHistory();
           wv.clearCache(true);
           wv.loadUrl("about:blank");
           wv = null;

这篇关于android webview不会在片段中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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