android - 为什么 Activity.getPackageManager() 会返回 null

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

我有将 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() 生命周期方法。

最佳答案

您没有通过 Intent 操作或明确 Intent 指定 Intent 。你可以试试这个:

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

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

关于android - 为什么 Activity.getPackageManager() 会返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005713/

相关文章:

android - 您通常如何向 Android 中的当前 Activity 添加 View ?

java - 当您调用异步任务的方法时,应用程序退出

java - HTML 到文本,带有自定义链接点击监听器

Android onClick 阻塞 onFling

android - 使用 Intents 作为消息传递的一种形式

android - 将 Activity Context 存储在 DialogFragment 中会导致内存泄漏吗?

java - 如何修复: error: not an enclosing class: Context

android - 从电子邮件链接启动 Activity

android - 从 DatePickerDialog 中删除年份

android - 使用脸部滤镜录制视频