6

I have method that resolves an Intent to a ComponentName:

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

Rarely, I get the following ACRA crash report:

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)

That line in the Intent class gets the NPE because PackageManager is null.

I see this infrequently, but frequently enough that made me write this question.

Does anybody have any idea, under what circumstances can Context/Activity.getPackageManager() return null? In my case, context is the Activity object itself, not the ApplicationContext. The app is executing the Activity.onResume() lifecycle method when this happens.

Tom anMoney
  • 1,231
  • 1
  • 14
  • 29

1 Answers1

0

You did not specify the intent with an intent action or an explicit intent. You can try this:

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

and then call the package manager to check that those applications or features are available on your device.

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
Omar Faroque Anik
  • 2,531
  • 1
  • 29
  • 42