首页 > 解决方案 > 当它要在浏览器中加载 URL 时出现“没有找到处理 Intent 的活动”错误

问题描述

我已经检查了有关No Activity found to handle Intent错误的类似问题。他们都没有解决我的问题。

当应用程序要使用AppCompatActivity中的 Intent 在 Chrome 浏览器中打开 URL 时,我的 Sentry 日志中出现此错误:

android.content.ActivityNotFoundException: No Activity found to handle Intent
{ act=android.intent.action.VIEW dat=https://www.example.com/...
flg=0x10000000 pkg=com.android.chrome }

这是我的代码:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");

if (MyMethods.isAppInstalled(getApplicationContext(), "com.android.chrome")) {
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException ex) {
        SentryLog.warn(ex);
        // Chrome browser presumably not installed so allow user to choose instead
        intent.setPackage(null);
        startActivity(intent);
    }
} else {
    // Chrome browser presumably not installed so allow user to choose instead
    intent.setPackage(null);
    startActivity(intent);
}

SentryLog.warn(ex);已报告错误。

这是isAppInstalled()MyMethods 类中的方法:

public static boolean isAppInstalled(Context context, String packageName) {
    try {
        if (context != null) {
            context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        }
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        MyLog.w(TAG, new Throwable().getStackTrace()[0].getLineNumber(), e);
        e.printStackTrace();
    }

    return false;
}

有时它会捕捉范围。如您所见,我检查了 chrome 是否安装在设备上,所以如果它不去其他地方,它就会安装!在这种情况下,为什么它无法执行startActivity(intent);并去捕获范围?

‍ 我的代码在一个Activity中,我应该使用intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);还是不使用?

标签: androidandroid-intentactivitynotfoundexception

解决方案


无需检查 chrome 它会找到浏览器本身。注意 url 必须以 http 或 https 开头。

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
fragmentActivity.startActivity(browserIntent);

推荐阅读