简体   繁体   English

解析URL的意图有时为null,抛出NullPointerException

[英]Resolving intent for URL is sometimes null, throws NullPointerException

I have an Android app that does this: 我有一个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();
    }

I have tested this on 4 different handsets and many different emulator configurations. 我已经在4种不同的手机和许多不同的仿真器配置上进行了测试。 All of them work fine. 所有这些都很好。 But I'm getting crash reports from deployed applications in the wild throwing an NPE since resolveInfo is sometimes null. 但我正在从野外部署的应用程序中获取崩溃报告,因为resolveInfo有时为null。

The immediate fix is to catch the NPE and deal with it before it crashes my app (as done above). 立即修复是在崩溃我的应用程序之前捕获NPE并处理它(如上所述)。 But I've tried for days to reproduce this myself and cannot. 但我已经尝试了几天自己重现这一点,但不能。 On an emulator with a single default browser installed, it resolves to com.android.browser.BrowserActivity: 在安装了单个默认浏览器的模拟器上,它解析为com.android.browser.BrowserActivity:

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

Likewise, on a handset with multiple browsers installed, this resolves somewhat differently...to com.android.internal.app.ResolverActivity: 同样,在安装了多个浏览器的手机上,这有点不同......对于com.android.internal.app.ResolverActivity:

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

The resolveActivity() docs here state: 该resolveActivity()文档在此状态:

Returns a ResolveInfo containing the final activity intent that was determined to be the best action. 返回ResolveInfo,其中包含被确定为最佳操作的最终活动意图。 Returns null if no matching activity was found. 如果未找到匹配的活动,则返回null。 If multiple matching activities are found and there is no default set, returns a ResolveInfo containing something else, such as the activity resolver. 如果找到多个匹配活动且没有默认设置,则返回包含其他内容的ResolveInfo,例如活动解析程序。

So I have covered the first scenario, that of an activity intent determined to be the best action. 所以我已经介绍了第一个场景,即被确定为最佳动作的活动意图。 Also the last scenario, that of multiple matching activities with no default action, returning the ResolverActivity. 最后一个场景,即没有默认操作的多个匹配活动,返回ResolverActivity。

I would really like to reproduce the scenario of resolveActivity() giving me a null when requesting the activity of a URL. 我真的想重现resolveActivity()的场景,在请求URL的活动时给我一个null。 Does anyone have any ideas how this can be done without, say, rooting the device? 有没有人知道如何在没有设备生根的情况下完成这项工作?

I would really like to reproduce the scenario of resolveActivity() giving me a null when requesting the activity of a URL. 我真的想重现resolveActivity()的场景,在请求URL的活动时给我一个null。 Does anyone have any ideas how this can be done without, say, rooting the device? 有没有人知道如何在没有设备生根的情况下完成这项工作?

  1. Go to Settings > Apps > ALL > Locate the browser(s) (on Nexus 4 with stock ROM that'd be Chrome) > Disable 转到设置>应用程序>全部>找到浏览器(在带有Chrome的库存ROM的Nexus 4上)>禁用
  2. Start catching NPEs :) 开始捕捉NPE :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM