java - URL 的解析 Intent 有时为 null,抛出 NullPointerException

标签 java android android-intent android-browser resolver

我有一个 Android 应用可以执行此操作:

    PackageManager pm = getApplicationContext().getPackageManager();
    Intent browserIntent = new Intent();
    browserIntent.setAction(Intent.ACTION_VIEW);
    browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
    browserIntent.setData(Uri.parse("http://www.google.com"));

    ResolveInfo resolveInfo = pm.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
    try {
        String browserType = nutent.activityInfo.packageName;
    } catch (NullPointerException npe) {
        npe.printStackTrace();
    }

我已经在 4 种不同的手机和许多不同的模拟器配置上对此进行了测试。他们都工作正常。但是由于 resolveInfo 有时为 null,我从野外部署的应用程序中获取崩溃报告并抛出 NPE。

立即修复是在它使我的应用程序崩溃之前捕获 NPE 并处理它(如上所述)。但是我已经尝试了好几天自己重现这个但不能。在安装了单个默认浏览器的模拟器上,它解析为 com.android.browser.BrowserActivity:

ResolveInfo{411ef228 com.android.browser.BrowserActivity p=0 o=0 m=0x208000}

同样,在安装了多个浏览器的手机上,这会有所不同...解析为 com.android.internal.app.ResolverActivity:

ResolveInfo{415c13b8 com.android.internal.app.ResolverActivity p=0 o=0 m=0x0}

resolveActivity() 文档 here状态:

Returns a ResolveInfo containing the final activity intent that was determined to be the best action. Returns null if no matching activity was found. If multiple matching activities are found and there is no default set, returns a ResolveInfo containing something else, such as the activity resolver.

所以我已经介绍了第一种情况,即确定为最佳操作的 Activity Intent 。还有最后一个场景,即没有默认操作的多个匹配 Activity ,返回 ResolverActivity。

我真的很想重现 resolveActivity() 在请求 URL 的 Activity 时给我一个 null 的场景。有没有人知道如何在不对设备进行 root 操作的情况下做到这一点?

最佳答案

I would really like to reproduce the scenario of resolveActivity() giving me a null when requesting the activity of a URL. Does anyone have any ideas how this can be done without, say, rooting the device?

  1. 转到“设置”>“应用”>“所有”> 找到浏览器(在 Nexus 4 上,带有 Chrome 的库存 ROM)> 禁用
  2. 开始捕捉 NPE :)

关于java - URL 的解析 Intent 有时为 null,抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995809/

相关文章:

java - 将 String 从 Intent 转换为 int

Activity 背景后的Android返回堆栈

java - Intent 崩溃应用程序

java - 从字符串中删除 eed 的正则表达式

java - 如何让 RestTemplate 使用 UriComponents 和 EncodingMode.VALUES_ONLY 对所有字符进行编码?

java - FAILURE 构建失败并出现异常

android - Python、Kivy、Buildozer : error using python-firebase/requests

Java 2D 着色/填充

java - 如何使用 3 个不同的文件选择器获取文件的 URI?

android - 层次结构查看器结果解释