title: Creating a spinner dynamically
body: <p>I was struggling to create a spinner dynamically. I had it on the page but every time I tried to select an option it would blow up. My original code is at the bottom. I fixed it by moving the <code>addSpinner()</code> function outside of the inner class and changing</p>
  
  <pre><code>Spinner newSpinner = new Spinner(getApplicationContext());
  </code></pre>
  
  <p>to</p>
  
  <pre><code>Spinner newSpinner = new Spinner(this);
  </code></pre>
  
  <p>It's fixed but I have no idea what it didn't work initially. Can anyone explain? (apologies if it's a noob question - I am new to both Java and android)</p>
  
  <pre><code>public class SpotManageActivity extends Activity
  {
      private SimpleCursorAdapter mSpots;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.reminder_manage_activity);
  
          Button add_new_button = (Button) findViewById(R.id.add_new_spot_button);
          add_new_button.setOnClickListener(new ButtonOnClickListener());
  
      }
  
      public class ButtonOnClickListener implements View.OnClickListener
      {
          @Override
          public void onClick(View v)
          {
              addSpinner();
          }
  
          private void addSpinner()
          {
              LinearLayout layoutHolder = 
                  (LinearLayout) findViewById(R.id.layout_holder);
  
              LinearLayout spinnerHolder = new LinearLayout(getApplicationContext());
              spinnerHolder.setOrientation(LinearLayout.HORIZONTAL);
  
              spinnerHolder.setLayoutParams(
                  new LinearLayout.LayoutParams(
                      LinearLayout.LayoutParams.FILL_PARENT, 
                      LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
              Spinner newSpinner = new Spinner(getApplicationContext());
              newSpinner.setLayoutParams(
                  new Spinner.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
                      ViewGroup.LayoutParams.WRAP_CONTENT));
              newSpinner.setAdapter(mSpots);
  
              layoutHolder.addView(spinnerHolder);
              spinnerHolder.addView(newSpinner);
  
              // A non-internal spot was selected
          }
  
      }
  
  }
  </code></pre>