【发布时间】: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