My code:
public class SpeedZonesFragment extends Fragment implements OnItemSelectedListener {
Spinner spinner;
... // Declaring onCreate and so on
spinner = (Spinner) view.findViewById(R.id.chosen_zone);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(MainActivity.this, R.array.speed_zones, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() // the program continues
Raises the error:
No enclosing instance of the type MainActivity is accessible in scope
I know why it does this but not how to fix it.
I have the MainActivity which does nothing but handle tabs a ViewPager.
I have tried to use .createFromResource with no luck.
I know the solution might be basic but after a few hours of browsing for solutions and trying different approaches to implement the OnItemSelectedListener i simply can’t find the answer. What am i missing for either scoping MainActivity or implementing the adapter differently (in order to get the Listener to work)?
Instead of
MainActivity.this, you can dogetActivity()in your fragment.