0

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;
}
  • 1
    Its going to be hard to help without more information. – Jason Crosby Oct 12 '21 at 15:32
  • @a_local_nobody no unfortunately – Moataz Mohamed Oct 12 '21 at 15:41
  • @JasonCrosby sorry it's my first time asking a question here, what more information should I provide? – Moataz Mohamed Oct 12 '21 at 15:41
  • 1
    we can't help without any crash logs, that post will help you find it and post it here. chances are, you find the logs, search for the cause and find an answer – a_local_nobody Oct 12 '21 at 15:42
  • @a_local_nobody will the logcat help even if it doesn't crash on my emulator and continues to function properly? I get some red warnings but not like the ones found on the other question you linked to – Moataz Mohamed Oct 12 '21 at 15:48
  • if it crashes then there's a stack trace, it's nearly impossible for us to help without it, unfortunately, because we won't know what caused the crash – a_local_nobody Oct 12 '21 at 15:56
  • 1
    This line is suspect: `spinner.setSelection(adapter.getPosition(preferences.getString("platform", "PC")));` the adapter returns "-1" if string not found which when provided to `setSelection` would likely be an index of out bounds. Your spinner possibilities are based on resources and the preferences may have be "preexisting" (from earlier version?) or perhaps PC is not valid - in either case you should check for -1 before setting selection. –  Oct 12 '21 at 16:43

0 Answers0