I published my first Android app about 2 weeks ago and some of the users are complaining about it crashing when accessing the Settings Fragment,
I never saw the bug on the emulator or my phone but what I'm suspecting is something to do with trying to access the shared preferences in the onCreateView() method
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
spinner = view.findViewById(R.id.platforms);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.platforms_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
SharedPreferences preferences = this.getActivity().getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedPlatform = spinner.getSelectedItem().toString();
SharedPreferences.Editor editor = preferences.edit();
editor.putString("platform", selectedPlatform);
editor.apply();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner.setSelection(adapter.getPosition(preferences.getString("platform", "PC")));
return view;
}