【发布时间】:2016-01-24 19:57:57
【问题描述】:
我尝试在 android 中使用 try 和 catch 块创建一种在没有互联网连接或网络服务器关闭的情况下进行处理的方法。在 Eclipse 中,IOException 带有红色下划线。如果http://192.168.0.23/loc/index.php 的加载失败,"file:///android_asset/myerrorpage.html" 应该会被加载。我只知道从 php 中尝试和捕获,实际上看过任何教程但找不到我的错误。
它显示以下消息:
IOException 无法到达的 catch 块。 try 语句体永远不会抛出这个异常
我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_localy);
mWebView = (WebView) findViewById(R.id.webview);
// Brower niceties -- pinch / zoom, follow links in place
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.setWebViewClient(new GeoWebViewClient());
// Below required for geolocation
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.setWebChromeClient(new GeoWebChromeClient());
// Load google.com
try {
mWebView.loadUrl("http://192.168.0.23/loc/index.php");
}
catch (IOException e) {
mWebView.loadUrl("file:///android_asset/myerrorpage.html");
}
}
【问题讨论】:
标签: java android web-applications webview try-catch