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 に答える