View Pager context issues

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(;

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;

    protected void onCreate(Bundle savedInstanceState) {
        mAdapter = new MyAdapter(getSupportFragmentManager());
        mVP = (ViewPager) findViewById(;
        mSpinner = (Spinner) findViewById(;

    public static class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fm) {

        public int getCount() {
            return 5;

        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");


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(;

I hope it helps!

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(;

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

Need Your Help

python get module variable by name

python variables module

I'm trying to find a way to access module variable by name, but haven't found anything yet. The thing I'm using now is:

Backpropagation and batch training

batch-file machine-learning neural-network backpropagation

Backpropagation calculates dW (weight delta) per weight per pattern, so it's straightforward how to modify weights when doing stochastic training. How do I use it for batch training, though? Simply

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.