java - 如何在 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 代码然后它将打开
解决方案
将http更改为https,这是唯一的问题。之后您的代码将运行良好。有关如何使用 webviews 的更多信息,请查看:https ://developer.android.com/reference/android/webkit/WebView
推荐阅读
- c# - 如何在内存中模拟仅在运行时持续的数据库?
- swift - 当它是存储属性时,可以在堆上分配 Swift 结构吗?
- r - rgdax(coinbase)数据未按预期收集数据
- python - Python 电子邮件发送给多个收件人
- c++ - 虚空、类、构造函数、多态性——执行顺序
- mysql - 使用 nodemailer 获取 SQL 数据并发送多封电子邮件?
- ios - React Native 多行垂直居中文本就 IOS 麻烦
- c# - 在 XAML 设计器中找不到 DataContext 类(虽然一切看起来都很好)
- c# - 调整波形文件音量时出现 NAudio 错误
- angular - 语音识别回调 this 未在回调中定义