我正在尝试使用WebView在我的应用程序中显示网页。起初,我在AndroidManifest.xml中没有任何权限,我收到错误'无法加载的网页因为net :: ERR_CACHE_MISS'
但是现在我已经将权限添加到AndroidManifest.xml中,WebView将作为空白框加载。
这是我的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>
我的Java课程:
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;
}
}
答案 0 :(得分:0)
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;
}
}
答案 1 :(得分:0)
使用以下代码
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);
}
}