Android “你好,WebView”示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2932469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Hello, WebView" example
提问by arakn0
I'm new in android development and I am trying out the WebView example in the official android site.
我是 android 开发的新手,我正在尝试官方 android 站点中的 WebView 示例。
http://developer.android.com/guide/tutorials/views/hello-webview.html
http://developer.android.com/guide/tutorials/views/hello-webview.html
But I do everything they say...which is pretty simple: I create the project, edit the layout file, then I add the code, etc. No problems building...but when I launch the app in the simulator I just got a black screen. It is like if the Layout is empty...like if the WebView is not created.
但我做了他们所说的一切......这非常简单:我创建项目,编辑布局文件,然后添加代码等。构建没有问题......但是当我在模拟器中启动应用程序时,我刚刚得到黑屏。就像 Layout 是空的……就像没有创建 WebView 一样。
What am I doing wrong?
我究竟做错了什么?
回答by Roman Nurik
Sorry about that – that link is a bit outdated. The fixed version of this tutorial is available here:
抱歉——那个链接有点过时了。本教程的固定版本可在此处获得:
http://developer.android.com/guide/webapps/webview.html
http://developer.android.com/guide/webapps/webview.html
We should remove the old link; I'll file a bug.
我们应该删除旧链接;我会提交一个错误。
And note, the error is that setContentView
isn't being called.
请注意,错误setContentView
是没有被调用。
回答by Karthi
in oncreate method add WebView.enablePlatformNotifications();
在 oncreate 方法中添加 WebView.enablePlatformNotifications();
in manifest file add
在清单文件中添加
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
now it works fine...
现在它工作正常......
回答by Sayanora Inc
webview_id = (WebView)findViewById(R.id.webview_id);
webview_id.getSettings().setJavaScriptEnabled(true); // enable javascript
WebSettings webSettings = webview_id.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webview_id.setInitialScale(90);
webSettings.setLoadWithOverviewMode(true);
webview_id.requestFocusFromTouch();
webview_id.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Snackbar.with(getApplicationContext()).dismiss();
Snackbar.with(getApplicationContext()) // context
.text(description) // text to display
.show(MainActivity.this);
}
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
});
if(isNetworkAvailable()){
webview_id .loadUrl("http://helloworld.org/");
}else{
Snackbar.with(getApplicationContext()).dismiss();
Snackbar.with(getApplicationContext()) // context
.text("Please Check your Internet Connection") // text to display
.show(MainActivity.this);
progressBar.setVisibility(View.VISIBLE);
}
}