【问题标题】:error to startActivity(intent), whats going wrong?startActivity(intent) 出错,怎么回事?
【发布时间】:2013-03-09 02:46:21
【问题描述】:

下面的代码 startActivity(intent) 给了我一个错误

这是我的代码:

public class MyWebViewClient3 extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.facebook.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // 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);//this is where it goes wrong
        return true;
        }
}

【问题讨论】:

  • 这是什么错误兄弟..?编译时间..?运行时异常..?
  • 试过“http://”它不工作...
  • 不能从 WebViewClient 调用 startActivity。
  • @ngesh:方法 startActivity(Intent) 未定义 MyWebViewClient 类型
  • @santhosh:是的,我在其他示例中尝试过这个,但它不适用于这个,我不明白为什么会这样......

标签: android url android-intent webview tabs


【解决方案1】:

WebViewClient 客户端不是 context ,因此您无法从此处启动 Activity。您可能希望获取 Context 作为参考,然后说

context.startActivity(intent);

vmironov的建议之后..

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.facebook.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // 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));
        view.getContext().startActivity(intent);
        return true;
    }

【讨论】:

  • 您可以从传递给WebViewContext 获得shouldOverrideUrlLoading
  • thnx thts so great u all it really works for me...真的thnx .. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多