【问题标题】:Why would Activity.getPackageManager() return null为什么 Activity.getPackageManager() 会返回 null
【发布时间】:2013-06-05 00:52:24
【问题描述】:

我有将 Intent 解析为 ComponentName 的方法:

private static ComponentName resolveViewExternalIntent(Context context)
{
    Intent intent = createIntent();
    return intent.resolveActivity(context.getPackageManager());
}

我很少收到以下 ACRA 崩溃报告:

java.lang.NullPointerException
    at android.content.Intent.resolveActivity(Intent.java:4518)
    at com.mypackage.myclass.resolveViewExternalIntent(SourceFile:271)
    at om.mypackage.myActivity.onResume(SourceFile:517)

Intent 类中的那一行获得 NPE,因为 PackageManager 是 null

我很少看到这种情况,但足够频繁地让我写下这个问题。

有没有人知道,在什么情况下 Context/Activity.getPackageManager() 可以返回null?在我的例子中,context 是 Activity 对象本身,而不是 ApplicationContext。发生这种情况时,应用正在执行 Activity.onResume() 生命周期方法。

【问题讨论】:

  • 您的上下文确定不为空?
  • 如果是,我会得到那个堆栈跟踪吗?所以,不,上下文不能为空。
  • 另外,我称之为resolveViewExternalIntent(this),其中'this'是Activity,所以它不可能为null。

标签: android android-activity android-context android-package-managers


【解决方案1】:

您没有通过意图操作或显式意图指定意图。你可以试试这个:

 Intent i = new Intent(Intent.ACTION_ZZZ); // Replace ZZZ

然后调用包管理器检查这些应用程序或功能在您的设备上是否可用。

【讨论】:

    猜你喜欢
    • 2013-08-09
    • 2014-05-23
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多