How To Add WebView In Fragments

问题: Here Is Fragment_Home.java package com.krishnakingofrestoration.webpark.webpark; import android.os.Bundle; import android.support.annotation.NonNull; import android.supp...

问题:

Here Is 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);




}}

And Its My 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>

When I Start The App Its Showing Me an Error Massge Onforchuntly App Has Stoped But I Removed The Webview Code Then It will Open


回答1:

Here are some things I noticed about your code:

  • You are using http instead of https. (Probably the main reason for your crash)
  • You are loading the url before setting the WebChromeClient.
  • You did not set a WebViewClient.

Based on this, I came up with a little code fix for you. Replace your webView logic with all of this:

webView = webView.findViewById(R.id.web_home);

WebSettings webSettings= webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new MyWebViewClient());

webView.loadUrl("https://google.com");

I hope this helps.. Merry coding!


Update:

All of this would not work if your internet permissions are not set. Go to your manifest file and add the line below in your permissions:

 <uses-permission android:name="android.permission.INTERNET" />

回答2:

Change http to https, that is the only problem. Your code will run fine after. For more information on how to use webviews, look at this: https://developer.android.com/reference/android/webkit/WebView


回答3:

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

View v=inflater.inflate(R.layout.fragment_bugtracker, container, false);
mWebView = (WebView) v.findViewById(R.id.webview);
mWebView.loadUrl("https://google.com");

// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());

return v;
 }

I Have Tried This and It Worked

  • 发表于 2019-03-23 04:41
  • 阅读 ( 198 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除