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
Here are some things I noticed about your code:
http
instead of https
. (Probably the main reason for your crash) 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!
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" />
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
@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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.