1

我是为 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/");
    }

}
4

1 回答 1

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;
        }

    }
}
于 2011-05-25T13:45:18.177 回答