首页 > 解决方案 > 如何在 Fragments 中添加 WebView

问题描述

这是 Fragment_Home.java

package com.krishnakingofrestoration.webpark.webpark;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Fragment_Home extends Fragment {
@Nullable
private WebView webView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      webView = webView.findViewById(R.id.web_home);
    webView.loadUrl("http://google.com");
    WebSettings webSettings= webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());
    return inflater.inflate(R.layout.fragment_home,null);




}}

它是我的 XML fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<WebView
    android:id="@+id/web_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />
 </RelativeLayout>

当我启动应用程序时它向我显示一个错误 Massge Onforchuntly 应用程序已停止但我删除了 Webview 代码然后它将打开

标签: javaandroidandroid-fragmentsandroid-fragmentactivity

解决方案


将http更改为https,这是唯一的问题。之后您的代码将运行良好。有关如何使用 webviews 的更多信息,请查看:https ://developer.android.com/reference/android/webkit/WebView


推荐阅读