我想知道当我把这段代码添加到我的代码中时有什么问题。
web.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
web.loadUrl("file:///android_asset/error.html");
}});
Progressbar
加载完成后,textview
不会变为不可见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);
}
}
最佳答案
1.)web.setWebViewClient(new WebViewClient() {
将覆盖上一个集合webviewnew 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
,因为根据惯例,类必须有第一个字母作为大写。