title: Where to create Android Spinner in Fragment?
body: <p>My question is rather simple: how can one use(populate) <code>Spinners</code> in <code>Fragments</code>? Or better said, what is wrong with my code below? Adding <code>Spinner</code> as I did is as simple as it gets, but after trying in all the different ways, nothing worked. What is <code>FragmentPagerAdapter</code> has to do with <code>Spinner</code>? If I add the <code>Spinner</code> from a method declared somewhere else, the <code>spinner</code> is populated with no problems(for example if populating the <code>spinner</code> from a <code>button</code>).</p>
  
  <p>Thanks in advance</p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
  /**
   * The {@link android.support.v4.view.PagerAdapter} that will provide
   * fragments for each of the sections. We use a
   * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
   * will keep every loaded fragment in memory. If this becomes too memory
   * intensive, it may be best to switch to a
   * {@link android.support.v4.app.FragmentStatePagerAdapter}.
   */
  SectionsPagerAdapter mSectionsPagerAdapter;
  public List&lt;String&gt; fragments = new Vector&lt;String&gt;();
  
  /**
   * The {@link ViewPager} that will host the section contents.
   */
  ViewPager mViewPager;
  private MySQLite database;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      // Create the adapter that will return a fragment for each of the three
      // primary sections of the app.
      mSectionsPagerAdapter = new SectionsPagerAdapter(
              getSupportFragmentManager());
  
      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.pager); // pager
      mViewPager.setAdapter(mSectionsPagerAdapter);
  
      // We initialise the database
      database = new MySQLite(this);
  
      Spinner spin = (Spinner)findViewById(R.id.spinner1);
      List&lt;String&gt; toSpin = new ArrayList&lt;String&gt;();
      toSpin.add("ONE");
      toSpin.add("TWO");
      toSpin.add("THREE");
      ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_spinner_item,toSpin);
      spin.setAdapter(adapter);
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  /**
   * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
   * one of the sections/tabs/pages.
   */
  
  public class SectionsPagerAdapter extends FragmentPagerAdapter {
  
      public SectionsPagerAdapter(FragmentManager fm) {
          super(fm);
          fragments.add(ConnectionFragment.class.getName());
          fragments.add(DataFragment.class.getName());
  
      }
  
      @Override
      public Fragment getItem(int position) {
          // we need to instantiate the list of fragments
          return Fragment.instantiate(getBaseContext(),
                  fragments.get(position));
  
      }
  
      @Override
      public int getCount() {
          // Show 3 total pages.
          return 2;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          Locale l = Locale.getDefault();
          switch (position) {
          case 0:
              return getString(R.string.title_section1).toUpperCase(l);
          case 1:
              return getString(R.string.title_section2).toUpperCase(l);
          }
          return null;
      }
  }
  
  /**
   * A dummy fragment representing a section of the app, but that simply
   * displays dummy text.
   */
  public static class ConnectionFragment extends Fragment {
      /**
       * The fragment argument representing the section number for this
       * fragment.
       */
      public static final String ARG_SECTION_NUMBER = "section_number";
  
      public ConnectionFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View connectionView = inflater.inflate(
                  R.layout.fragment_main_dummy, container, false);
          return connectionView;
      }
  }
  
  public static class DataFragment extends Fragment {
  
      public DataFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View dataView = inflater.inflate(R.layout.fragment_linear,
                  container, false);
          return dataView;
      }
  }
  </code></pre>
  
  <p>Logcat:</p>
  
  <pre><code>11-16 18:26:13.092: E/AndroidRuntime(16442): FATAL EXCEPTION: main
  11-16 18:26:13.092: E/AndroidRuntime(16442): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iwallet/com.example.iwallet.MainActivity}: java.lang.NullPointerException
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at android.os.Handler.dispatchMessage(Handler.java:99)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at android.os.Looper.loop(Looper.java:213)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at android.app.ActivityThread.main(ActivityThread.java:4787)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at java.lang.reflect.Method.invokeNative(Native Method)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at java.lang.reflect.Method.invoke(Method.java:511)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at dalvik.system.NativeStart.main(Native Method)
  11-16 18:26:13.092: E/AndroidRuntime(16442): Caused by: java.lang.NullPointerException
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at com.example.iwallet.MainActivity.onCreate(MainActivity.java:65)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at android.app.Activity.performCreate(Activity.java:5008)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
  11-16 18:26:13.092: E/AndroidRuntime(16442):    ... 11 more
  </code></pre>