can someone please tell me why in the following line the code throws a NullPointer.
public List<ApplicationInfo> getAppInfos(Context myContext) {
PackageManager pm = myContext.getPackageManager();
List<ApplicationInfo> appInfo = pm.getInstalledApplications(PackageManager.GET_META_DATA);
Collections.sort(appInfo, new ApplicationInfo.DisplayNameComparator(pm)); //throws Nullpointer
return appInfo;
}
I try to call this method from a seperate Thread (not the UIThread). But i passed the Context from my Activity... If i call this method from my Activity(without passing the context for sure) it works fine.
I appreciate any help.
Update regarding Comment:
Thank you for your respone. That's a tricky Thing. Because the Code runs fine on Emulator with API 17 but not on my real device. And i got a cool Alcatel phone which is, for me, really tricky to get it run for usb Debugging(i think not just for me, the Support is awful). Under some circumstances i achieved to Crash the app on the Emulator as well. I only remember a nullpointerexception, but maybe i mixed something. But what i can say is that when i comment the Collections.sort... out. The app runs on the device as well. I'm sorry for being so confusing right now. The only Thing i can say is that this line crashes my app on my device, but not on my Emulator and only if i call this from an seperate Thread.
I instantiate Context like this:
final Context myContext = MyActivity.this;
And in the Thread i just call the method like this:
List<ApplicationInfo> myList = getAppInfos(myContext);
PHONE and Emulator are running with same Android Version!