在android studio中,我有MainActivity.java的代码,当它没有连接到互联网时,打开错误页面“ error.html”,一切正常....但是在我的wordpress中,我有一个插件在弹出/框外部页面中打开的名为“ WP Colorbox”的文件..但是当我运行该应用程序并使用此插件打开某些文件时,我的error.html并没有出现,但是当互联网关闭时,却出现了常见错误“ net :: ERR_ADRESS_UNREACHABLE “
在这种情况下,我该怎么办才能显示error.html?
最佳团员
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
SwipeRefreshLayout swipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
LoadWeb();
}
});
LoadWeb();
}
public void LoadWeb(){
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.loadUrl("http://www.myWEBSITE.com");
swipe.setRefreshing(true);
webView.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
public void onPageFinished(WebView view, String url){
//Hide the SwipeReefreshLayout
swipe.setRefreshing(false);
}
});
}
@Override
public void onBackPressed(){
if (webView.canGoBack()){
webView.goBack();
}else {
finish();
}
}
}