This is my first time using fragments (new to android dev) and I'm trying to set up a spinner. At the moment I am quite confused about context and can't seem to solve this error:
Error:(52, 78) error: incompatible types: HotkeysFragment cannot be converted to Context
Here is the code its referring to:
HotkeysFragment.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
Spinner hotkey_selector_spinner = (Spinner) rootView.findViewById(R.id.hotkey_selector_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.hotkey_options, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hotkey_selector_spinner.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootViewB = inflater.inflate(R.layout.fragment_hotkeys, container, false);
rootView = rootViewB;
return rootViewB;
}
The specific line is:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.hotkey_options, android.R.layout.simple_spinner_item);
Imports:
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.zip.Inflater;
How can I fix this?
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)
@Override
public 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With