Part of my code is crashing for users only using Pixel/Pixel 2 phones on Android 9.0
. I've used Android Emulator to test as many situations as possible and have been unable to reproduce the reported NumberFormatException
.
Here is the code that the crash is referring to:
public void onRecieve(...) {
//...
int network_pref = Integer.parseInt(getDefaults("network_pref", context));
//...
}
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
The default value for the preference is "0", the only other option is "1".
It might be possible that you get default method is returning null, because that's the value you are passing.
You may try this and it may work...
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, "0");
}