如何在Android WebView应用程序中添加自定义错误页面而不出现任何错误?
以下代码是应用程序的主要活动。我尝试使用以下命令添加自定义错误页: mywebView.setWebViewClient(新的WebViewClient(){ @重写公共void onReceivedError(WebView视图、int errorCode、字符串描述、字符串failingUrl){ mywebView.loadUrl(“file:///android_asset/error.html"); } }); 当我在主活动中使用上述代码时,错误页面或加载图标一次都在工作。(相互覆盖) 我不明白我错在哪里了。有人能帮我解决这个问题吗?提前谢谢如何在Android WebView应用程序中添加自定义错误页面而不出现任何错误?,android,android-activity,webview,android-webview,custom-error-pages,Android,Android Activity,Webview,Android Webview,Custom Error Pages,以下代码是应用程序的主要活动。我尝试使用以下命令添加自定义错误页: mywebView.setWebViewClient(新的WebViewClient(){ @重写公共void onReceivedError(WebView视图、int errorCode、字符串描述、字符串failingUrl){ mywebView.loadUrl(“file:///android_asset/error.html"); } }); 当我在主活动中使用上述代码时,错误页面或加载图标一次都在工作。(相互覆盖)
public class MainActivity extends AppCompatActivity {
public WebView mywebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.loadUrl("http://google.com/");
mywebView.setWebViewClient(new WebViewClient());
mywebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
mywebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
findViewById(R.id.progress).setVisibility(View.VISIBLE);
}
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progress).setVisibility(View.GONE);
}
});
}
public void onBackPressed() {
if(mywebView.canGoBack()){
mywebView.goBack();
} else {
super.onBackPressed();
}
}}
最后我找到了解决办法。你的语法含义似乎与我没有正确理解你的意思不同。你错误地使用了单词syntax,所以你的问题没有意义(或者至少很难理解你的意思),但答案是:你应该在一个类中覆盖所有3个方法,而不是在两个不同的类中。如果你有更好的语法,请与我分享。我会改正的,正如我写的。。。一个类中的所有3个方法。。。太好了。这是我的朋友。thnx fr th idea.:)我重新格式化了我的问题。但你还没有重新启动它。
mywebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
findViewById(R.id.progress).setVisibility(View.VISIBLE);
}
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progress).setVisibility(View.GONE);
}
public void onReceivedError(WebView webview, int i, String s, String s1)
{
webview.loadUrl("file:///android_asset/error.html");
}
});