Java 致命异常:android.content.ActivityNotFoundException:未找到可处理意图的活动

Java 致命异常:android.content.ActivityNotFoundException:未找到可处理意图的活动,java,android,webview,chrome-custom-tabs,android-customtabs,Java,Android,Webview,Chrome Custom Tabs,Android Customtabs,打开url时,在某些设备上出现以下错误。我的设备上没有任何错误,但许多设备都有 Fatal Exception: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://google.com/... } at android.app.Instrumentation.checkStartActivityR

打开url时,在某些设备上出现以下错误。我的设备上没有任何错误,但许多设备都有

Fatal Exception: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://google.com/... }
   at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2076)
   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1720)
   at android.app.Activity.startActivityForResult(Activity.java:5258)
   at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
   at android.app.Activity.startActivityForResult(Activity.java:5203)
   at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:560)
   at android.app.Activity.startActivity(Activity.java:5587)
   at android.app.Activity.startActivity(Activity.java:5555)
发生错误的代码非常简单

Uri uri = Uri.parse("https://google.com");

try {
     CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
     builder.setShowTitle(true);
     CustomTabsIntent customTabsIntent = builder.build();
     Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));

     ResolveInfo resolveInfo = getPackageManager().resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
     if (resolveInfo != null && resolveInfo.activityInfo.packageName.length() > 0) {
         customTabsIntent.intent.setPackage(resolveInfo.activityInfo.packageName);
     }

     customTabsIntent.launchUrl(this, uri);
} catch (ActivityNotFoundException e) {
          // Do nothing
}
在某些设备(不是旧设备,大部分是最新设备)中,会触发异常,但这怎么可能呢?这意味着设备有0个浏览器?如果是这样,我的问题是,即使用户禁用了所有浏览器,如何打开url?提前谢谢

注意:在升级到live版本中的自定义选项卡之前,我使用以下代码打开url,但我始终获得ActivityNotFoundException:

这是打开任何url但不使用try/catch会导致崩溃的最简单代码。使用普通方式或自定义选项卡并不重要,它总是导致ActivityNotFoundException。我正在寻找一个解决方案,无论发生什么。我不确定这是否可能

注2:MinApi是Android 5.0(棒棒糖)


关于这个问题的一些注意事项:

将非浏览器应用检测为浏览器 他们查询浏览器是否可以检测到非浏览器应用程序,这将导致在启动自定义选项卡时出现
ActivityNotFoundException
。有关示例,请参见

查询可处理浏览器意图的包时,我建议使用以下选项:


意向浏览器内容=新意向()
.setAction(Intent.ACTION\u视图)
.addCategory(Intent.CATEGORY可浏览)
.setData(Uri.fromParts(“http”,“null”);
这也是为什么

以下内容应该不是问题,因为“自定义”选项卡仍然可以工作,但在Android M上,
PackageManager.MATCH_DEFAULT_ONLY
的行为发生了更改,并且只返回默认浏览器-我不确定如果未设置默认浏览器会发生什么,但这可能意味着返回的列表将变为空。这意味着当未设置默认浏览器时,用户将获得消歧对话框。在安卓M及以上版本中,您可能还需要使用
PackageManager.MATCH_ALL
查询软件包管理器。如果您希望获取所有浏览器,请避免使用消歧对话框

查看android浏览器帮助程序库中的
以检测自定义选项卡和受信任的Web活动

为Android 11做准备 Android 11已经引入,应用程序现在必须声明它们正在查询哪些包。如果应用程序未实现此功能,则即使安装了浏览器,查询包管理时返回的列表也将为空

if(Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.M){
可能的Providers.addAll(pm.QueryInputActivities(queryBrowsersIntent,
PackageManager.MATCH_ALL);
}
同样,这不会导致ActivityNotFoundException,因为它只会导致自定义选项卡在没有包集的情况下启动

禁用的浏览器 注意:在升级到live版本中的自定义选项卡之前,我使用以下代码打开url,但我始终获得ActivityNotFoundException:

Intent-browserint=newintent(Intent.ACTION\u视图,uri);
startActivity(浏览器内容);
用户可以禁用所有浏览器应用程序,但我不认为这是常见的


另外需要注意的是,Android TV和Android Auto设备没有安装浏览器。如果您的应用程序以这些设备为目标,您将无法打开这些URL。但这只是你的应用程序针对这些平台时的一个问题。

我根据@andreban的答案创建了一个方法。我相信它会打开url%99.99次

public static void openURL(Context mContext, Uri uri) {
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    builder.setShowTitle(true);
    CustomTabsIntent customTabsIntent = builder.build();
    Intent browserIntent = new Intent()
            .setAction(Intent.ACTION_VIEW)
            .addCategory(Intent.CATEGORY_BROWSABLE)
            .setType("text/plain")
            .setData(Uri.fromParts("http", "", null));

    List<ResolveInfo> possibleBrowsers;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        possibleBrowsers = mContext.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
        if (possibleBrowsers.size() == 0) {
            possibleBrowsers = mContext.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_ALL);
        }
    } else {
        possibleBrowsers = mContext.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
    }

    if (possibleBrowsers.size() > 0) {
        customTabsIntent.intent.setPackage(possibleBrowsers.get(0).activityInfo.packageName);
        customTabsIntent.launchUrl(mContext, uri);
    } else {
        Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, uri);
        mContext.startActivity(browserIntent2);
    }
}
publicstaticvoidopenurl(Context-mContext,Uri){
customtabsinent.Builder=新的customtabsinent.Builder();
建造商名称(真实);
CustomTabsInt CustomTabsInt=builder.build();
意向浏览器内容=新意向()
.setAction(Intent.ACTION\u视图)
.addCategory(Intent.CATEGORY可浏览)
.setType(“文本/普通”)
.setData(Uri.fromParts(“http”,“null”);
列出可能的律师;
if(android.os.Build.VERSION.SDK\u INT>=android.os.Build.VERSION\u code.M){
possibleBrowsers=mContext.getPackageManager().QueryInputActivities(仅限BrowserContent、PackageManager.MATCH_默认值_);
if(可能的browsers.size()==0){
possibleBrowsers=mContext.getPackageManager().QueryInputActivities(BrowserContent,PackageManager.MATCH_ALL);
}
}否则{
possibleBrowsers=mContext.getPackageManager().QueryInputActivities(仅限BrowserContent、PackageManager.MATCH_默认值_);
}
如果(可能的browsers.size()>0){
customtabsinent.intent.setPackage(可能的browsers.get(0.activityInfo.packageName));
启动URL(mContext,uri);
}否则{
Intent browserIntent2=新的意图(Intent.ACTION\u视图,uri);
mContext.startActivity(浏览器内容2);
}
}

这是否回答了您的问题?没有什么能阻止用户禁用所有浏览器应用,你必须处理好这一点,是的。你的问题到底是什么?@javdromero不,这不是chrome的问题。我只是尝试用自定义选项卡打开url,但它不必是chrome自定义选项卡。我只是查看任何应用程序以打开http,但找不到应用程序,因为触发了异常。@ianhanniballake即使用户禁用了所有浏览器,我如何打开url?我必须以某种方式打开链接。我将尝试使用PackageManager.MATCH_所有选项。我希望它能起作用。谢谢PackageManager.MATCH_都需要Android 6.0,所以我实现如下:ResolveInfo ResolveInfo;if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_code.M){resolveInfo=getPackageManager().resolveActivity(browserint,PackageManager.MATCH_ALL);}else{
public static void openURL(Context mContext, Uri uri) {
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    builder.setShowTitle(true);
    CustomTabsIntent customTabsIntent = builder.build();
    Intent browserIntent = new Intent()
            .setAction(Intent.ACTION_VIEW)
            .addCategory(Intent.CATEGORY_BROWSABLE)
            .setType("text/plain")
            .setData(Uri.fromParts("http", "", null));

    List<ResolveInfo> possibleBrowsers;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        possibleBrowsers = mContext.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
        if (possibleBrowsers.size() == 0) {
            possibleBrowsers = mContext.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_ALL);
        }
    } else {
        possibleBrowsers = mContext.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
    }

    if (possibleBrowsers.size() > 0) {
        customTabsIntent.intent.setPackage(possibleBrowsers.get(0).activityInfo.packageName);
        customTabsIntent.launchUrl(mContext, uri);
    } else {
        Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, uri);
        mContext.startActivity(browserIntent2);
    }
}