首页 文章

错误消息 - > shouldOverrideUrlLoading Webview Android

提问于
浏览
0

我需要解决Webview和方法ShouldOverrideUrlLoading的问题 .

我想显示一条消息,指出用户没有在手机上安装Twitter应用

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

  }

显示“应用程序意外停止的错误 . 请再试一次”

有人可以帮忙吗?

1 回答

  • 1

    您的Twitter Intent 似乎有问题 . 据我所知,你希望Twitter应用程序发布一些东西 . 请参阅他们的API,了解如何使用Intent启动Twitter应用程序 . 为防止崩溃,您可以执行以下操作:

    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;
    

相关问题