Why would Activity.getPackageManager() return null
Asked Answered
K

1

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.

Karttikeya answered 9/6, 2013 at 2:5 Comment(3)
Your context is not null for sure ?Educated
If it were, would I get that stack trace? So, no, context cannot be null.Karttikeya
Besides, I call this resolveViewExternalIntent(this) where 'this' is the Activity, so it's impossible it to be null.Karttikeya
T
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.

Turenne answered 14/4, 2014 at 19:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.