Tom*_*ney 5 android android-context android-package-managers android-activity
我有一个方法将Intent解析为ComponentName:
private static ComponentName resolveViewExternalIntent(Context context)
{
Intent intent = createIntent();
return intent.resolveActivity(context.getPackageManager());
}
Run Code Online (Sandbox Code Playgroud)
我很少得到以下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)
Run Code Online (Sandbox Code Playgroud)
Intent类中的那一行获取NPE,因为PackageManager是null.
我很少看到这一点,但经常这足以让我写下这个问题.
有没有人知道,在什么情况下Context/Activity.getPackageManager()会返回null?就我而言,context是Activity对象本身,而不是ApplicationContext.当发生这种情况时,应用程序正在执行Activity.onResume()生命周期方法.
您没有通过意图操作或显式意图指定意图。你可以试试这个:
Intent i = new Intent(Intent.ACTION_ZZZ); // Replace ZZZ
Run Code Online (Sandbox Code Playgroud)
然后调用程序包管理器来检查这些应用程序或功能在您的设备上是否可用。
| 归档时间: |
|
| 查看次数: |
3487 次 |
| 最近记录: |