A NullPointerException that seems impossible
I have the following code in a method. This is the entire method:
private boolean isPlaceEligible(String eligibleId) {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String current = sharedPrefs.getString(
PrefsActivity.AUTO_INELIGIBLE_ID, PrefsActivity.AUTO_INELIGIBLE_ID_DEF);
return !eligibleId.equals(current);
}
eligibleId is known to be a non-null String. PrefsActivity.AUTO_INELIGIBLE_ID is a String key, and PrefsActivity.AUTO_INELIGIBLE_ID_DEF is a default String for that key.
The JavaDocs for those SharedPreferences don't say anything about returning null, but I've had some crash reports from the Market Console that say that this method is crashing on a NullPointerException. Based on the number of reports I got, I would estimate that this is happening to about one out of 300 users, maybe less. Two of them have emailed me about it and said that it happens 100% of the time.
An开发者_开发技巧y ideas?
The problem is probably the passed argument as the context in this line:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
This seems as the solution:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
精彩评论