-2

Within my MainActivity, I am trying to get the context of a control (spinner) that is located on a fragment. I am using a FragmentStatePagerAdapter to manage the fragment(s). The last line of my onCreate() throws the exception because the mSpinner keeps returning a null. Ive tried that line two different ways, as shown and:

mSpinner = (Spinner) mVP.findViewById(R.id.spinner1);

I am assuming that my context is incorrect but don't have the experience to know how to fix without blindly guessing. Thanks for any constructive help you can offer in advance! :)

My code looks like this:

public class MainActivity extends FragmentActivity {
    MyAdapter mAdapter;
    ViewPager mVP;
    Spinner mSpinner;
    Spinner.OnItemSelectedListener onSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAdapter = new MyAdapter(getSupportFragmentManager());
        mVP = (ViewPager) findViewById(R.id.viewPager);
        mVP.setAdapter(mAdapter);
        mSpinner = (Spinner) findViewById(R.id.spinner1);
    }

    public static class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public Fragment getItem(int pos) {
            switch (pos) {

                case 0:return FirstFragment.newInstance("FirstFragment, Instance 1");
                case 1:return SecondFragment.newInstance("SecondFragment, Instance 1");
                case 2:return ThirdFragment.newInstance("ThirdFragment, Instance 1");
                case 3:return ThirdFragment.newInstance("ThirdFragment, Instance 2");
                case 4:return ThirdFragment.newInstance("ThirdFragment, Instance 3");
                default:return ThirdFragment.newInstance("ThirdFragment, Default");
            }
        }
    }
}
Cartoondog
  • 105
  • 1
  • 8
  • where is your logcat? – M D Aug 18 '15 at 05:25
  • that is obvious! u cant get access to the components of fragments that are handled by adapter , here in activity ..this way. for a workaround ..whatever u want to do with spinner..implement onpagechangeListener and call getadapter.getview.findViewbyid(id) and do whatever u want to do ..though that is an ugly workaround! – Jivraj S Shekhawat Aug 18 '15 at 05:44

2 Answers2

1

You can get Your Fragment view this way and then use Fragment's view to find Spinner using findViewById().

Try this.

View fragment_view = getLayoutInflater().inflate(R.layout.fragment_view, null);
Spinner mSpinner = (Spinner) fragment_view.findViewById(R.id.spinner1);

I hope it helps!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • Thanks. Whereas others want to be sarcastic and comment about what I am doing wrong, you have shown what to do right- awesome! – Cartoondog Aug 21 '15 at 20:57
1

I am trying to get the context of a control (spinner) that is located on a fragment.

This is not going to work:

mSpinner = (Spinner) findViewById(R.id.spinner1);

Because one of your fragments contains the spinner, so you have to get the spinner reference from the fragment, not from the activity!

László Magyar
  • 355
  • 1
  • 16