我想知道当我将这段代码添加到我的代码时会出现什么问题。
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
web.loadUrl("file:///android_asset/error.html");
}});
Progressbar
& textview
加载完成后,webview
永远不会隐藏
public class YazdMap extends ActionBarActivity {
WebView web;
ProgressBar progressBar;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yazd_map);
web = (WebView) findViewById(R.id.webview1);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
txt = (TextView) findViewById(R.id.txt);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://google.com/");
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
web.loadUrl("file:///android_asset/error.html");
}
});
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(view.GONE);
txt.setVisibility(view.GONE);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack())
{web.goBack();
return true;}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:3)
1。)web.setWebViewClient(new WebViewClient() {
将覆盖之前设置的网络视图new myWebClient()
,因此请删除此
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
web.loadUrl("file:///android_asset/error.html");
}
});
2。)将onReceivedError
移到myWebClient
课程中,以便代码
public class YazdMap extends ActionBarActivity {
WebView web;
ProgressBar progressBar;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yazd_map);
web = (WebView) findViewById(R.id.webview1);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
txt = (TextView) findViewById(R.id.txt);
web.setWebViewClient(new MyWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://google.com/");
}
public class MyWebClient extends WebViewClient
{
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(view.GONE);
txt.setVisibility(view.GONE);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
view.loadUrl("file:///android_asset/error.html");
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack())
{web.goBack();
return true;}
return super.onKeyDown(keyCode, event);
}
}
注意:我将名称从myWebClient
更改为MyWebClient
,因为类必须按照惯例将首字母作为大写字母。