安卓 : Simple web application crashes on links other then the http scheme

标签 android webview mailto tel

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

}

最佳答案

抱歉这么笨,这实际上解决了它。对于 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;
        }

    }
}

关于安卓 : Simple web application crashes on links other then the http scheme,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6125058/

相关文章:

Android webview 不播放视频,jwembedder

javascript - 如何从 Outlook 加载项创建新电子邮件?

css - Mailto:通过 CSS 几年前就停止工作了

android - 在字符串中使用变量,执行 Html.fromHtml

App更新的Android版本代码

android - 在 Android WebView 中渲染 Azure Blob 内容

javascript - 下一个单元格上的自动可点击电子邮件地址。应用程序脚本

android - 如何处理 SMS SENT 事件?

Android更新通知文本导致内存占用

android - 在 WebView 中下载文件