Fragment cannot be converted to Context
From the Android docs on Fragments from here:
Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null.
So, in addition to changing this
to getActivity()
, I also suggest that you work with getActivity()
in onActivityCreated()
(since you also need the view to be inflated first)
@Overridepublic void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } Spinner hotkey_selector_spinner = (Spinner) getView().findViewById(R.id.hotkey_selector_spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.hotkey_options, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); hotkey_selector_spinner.setAdapter(adapter);}
Change
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.hotkey_options, android.R.layout.simple_spinner_item);
to
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.hotkey_options, android.R.layout.simple_spinner_item);
Access context in fragment by using getActivity()
Fragment
can not be converted into Context
, an Activity
can.
So you should change
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.hotkey_options, android.R.layout.simple_spinner_item);
to
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.hotkey_options, android.R.layout.simple_spinner_item);