user2661416
user2661416

Reputation: 29

Error message -> shouldOverrideUrlLoading Webview Android

I need solved a problem with Webview and the method ShouldOverrideUrlLoading.

I want to display a message indicating that the user doesn't have the twitter app installed on your phone

    @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("tel:")) { 
                Intent intent = new Intent(Intent.ACTION_DIAL,
                        Uri.parse(url)); 
                startActivity(intent); 
        }else if(url.startsWith("http:") || url.startsWith("https:")) {
            view.loadUrl(url);
        }else if (url != null && url.startsWith("market://")) {
                view.getContext().startActivity(
                    new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
        } else if (url != null && url.startsWith("twitter://")) {
                view.getContext().startActivity(
                    new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
        }else{
    Toast.makeText(getApplicationContext(), "Twitter app is necessary", Toast.LENGTH_SHORT).show();
      }
            return false;

}

The error that show "The application has stopped unexpectedly. Please try again"

Can anyone help?

Upvotes: 1

Views: 2557

Answers (1)

Droidman
Droidman

Reputation: 11608

There seems to be a problem with your twitter Intent. As far as I understood you want the twitter app to post something. Consult their API's about how to launch twitter app using an Intent. To prevent crashes you might do the following:

 else if (url != null && url.startsWith("twitter://")) {
            try{
            view.getContext().startActivity(
                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
             } catch(ActivityNotFoundException e){
               // do some stuff here, for example load the twitter url in the browser
             }
            return true;

Upvotes: 1

Related Questions