开发者

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

开发者 https://www.devze.com 2023-03-08 02:47 出处:网络
I am a beginner in developing for the Android, so excuse me if this is obvious. I created a simple web application using the samples from android.com however, when a link like tel: or mailto: is click

I am a beginner in developing for the Android, so excuse me if this is obvious. I created a simple web application using the samples from android.com however, when a link like tel: or mailto: is clicked, the application crashes and askes me to force quit. I was wondering if anyone could tell me why.

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);
  开发者_如何学C  }

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

}


Sorry to be so stupid, this actualy fixed it. With mailto, there is no host, thus getHost() will return null and causes the exception.

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

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消