I'm trying to use a WebView to display a webpage in my application. At first, I didn't have any permissions in the AndroidManifest.xml and I got the error 'the webpage at could not be loaded because net::ERR_CACHE_MISS'
But now that I have added the permission to the AndroidManifest.xml, the WebView loads as a empty white box.
Here is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package= ... >
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</application>
</manifest>
And my Java class:
public class location_Fragment extends Fragment {
private WebView webView;
View rootview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.location_layout, container, false);
webView = (WebView)rootview.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");
return rootview;
}
}
Try this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.location_layout, container, false);
webView = (WebView)rootview.findViewById(R.id.webView);
webView.setWebViewClient(new MyBrowser());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");
return rootview;
}
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Use The Following Code
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Bundle b = getIntent().getExtras();
String data =b.getString("link");
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
if(data.equals("")||data.equals("null")) {
Toast.makeText(WebViewActivity.this,"No Link Available",Toast.LENGTH_SHORT).show();
}
{
webView.loadUrl(data);
}
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
// activity.setProgress(progress * 1000);
WebViewActivity.this.setTitle("Loading...");
WebViewActivity.this.setProgress(progress * 100);
if(progress == 100)
WebViewActivity.this.setTitle(R.string.app_name);
}
});
}
private class MyWebViewClient extends WebViewClient {
private ProgressDialog pDialog;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// pDialog = new ProgressDialog(WebViewActivity.this,R.style.DialogTheme);
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (WebView view, String url) {
if (pDialog == null) {
// in standard case YourActivity.this
pDialog = new ProgressDialog(WebViewActivity.this,R.style.DialogTheme);
pDialog.setMessage("Loading...");
pDialog.show();
}
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
public void onPageFinished(WebView view, String url) {
try{
pDialog.dismiss();
if (pDialog.isShowing()) {
pDialog.dismiss();
pDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
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.