我是开发Android的初学者,请原谅我,如果这是显而易见的。我使用android.com中的示例创建了一个简单的Web应用程序,然而,当点击tel:或mailto:这样的链接时,应用程序崩溃并要求我强行退出。我想知道是否有人能告诉我原因。
public class MyApplication extends Activity {
WebView mWebView;
private class InternalWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This is my web site, so do not override; let my WebView load the page
if (Uri.parse(url).getHost().equals("m.mydomain.nl")) {
return false;
} else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the WebView class to display website */
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new InternalWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://m.mydomain.nl/");
}
}
答案 0 :(得分:3)
很抱歉这么愚蠢,这实际上修好了。使用mailto,没有主机,因此getHost()将返回null并导致异常。
private class InternalWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This is my web site, so do not override; let my WebView load the page
if (Uri.parse(url).getHost() != null && Uri.parse(url).getHost().equals("m.domain.nl")) {
return false;
} else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}