android webview 不在片段中加载

android webview do not load in fragment

我面临一个非常非常奇怪的问题:我正在尝试在片段中加载 webview,但我发誓我找不到让它工作的方法,而且我对原因。奇怪的是,如果 运行 在 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>

这是代码工作的activity

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;
    }



}

这是代码不起作用的片段

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 将 wv 替换为 getWebView 但问题仍然存在

谢谢你的帮助

将所有代码从 onCreated(..) 移至 onCreateView(...)。并删除

super.onCreateView(inflater, container, savedInstanceState);

更正:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout.activity_prova_webview, null);

    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, "");

   return view;

}

输出:

我发现了问题:

我有另一个 Activity 显示嵌入了 youtube 视频的 WebView,并在关闭时停止它 Activity 我在完成方法中使用了这段代码

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

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

 wv.pauseTimers();

确认

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

wv.pauseTimers();

删除此行一切正常,让 youtube 视频停止播放此代码工作正常

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