title: How to create a Spinner dynamically in android?
body: <p>I used the HelloTabWidget from here (http://developer.android.com/resources/tutorials/views/hello-tabwidget.html) as a starting point.</p>
  
  <p>Now I edited the onCreate for the first Tab:</p>
  
  <pre><code>// Initialize a TabSpec for each tab and add it to the TabHost
  spec = tabHost.newTabSpec("tab0").setIndicator("Tab0", res.getDrawable(R.drawable.ic_tab_artists));
  spec.setContent(new MyTabContentFactory(this, R.layout.tab0));
  tabHost.addTab(spec);
  </code></pre>
  
  <p>MyTabContentFactory:</p>
  
  <pre><code>public class MyTabContentFactory implements TabContentFactory {
  
      private Activity parent = null;
      private int layout = -1;
  
      public MyTabContentFactory(Activity parent, int layout) {
          this.parent = parent;
          this.layout = layout;
      }
  
  
      @Override
      public View createTabContent(String tag) {
          View inflatedView = View.inflate(parent, layout, null);//using parent.getApplicationContext() threw a android.view.WindowManager$BadTokenException when clicking ob the Spinner.
          //initialize spinner
          CharSequence array[] = new CharSequence[4];
          for (int i = 0; i &lt; array.length; i++) {
              array[i] = "Element "+i;
          }
          ArrayAdapter&lt;CharSequence&gt; adapter = new ArrayAdapter&lt;CharSequence&gt;(parent, android.R.layout.simple_spinner_item, array);
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
          View view = parent.findViewById(layout);
          if(view != null) {
              ArrayList&lt;View&gt; touchables = view.getTouchables();
              for (View b : touchables) {
                  if (b instanceof Spinner) {
                      ((Spinner) b).setAdapter(adapter);
                  }
              }
          }
  
          return inflatedView;
      }
  }
  </code></pre>
  
  <p>tab0.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
      &lt;Spinner
          android:id="@+id/entry1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:prompt="@string/brand_prompt"
          /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>MyTabContentFactory should initialize the Spinner, but view in createTabContent is always null. Why is that so? How can I find the Spinner in order to initialize it?</p>