title: Error with a simple Spinner on Android
body: <p>I'm learning about Spinners in Android, and I've tested the code that appears in this link of Android Developers:
  <a href="http://developer.android.com/guide/topics/ui/controls/spinner.html" rel="nofollow">http://developer.android.com/guide/topics/ui/controls/spinner.html</a></p>
  
  <p>But it doesn't work, these are the files:</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>...
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
      // Create an ArrayAdapter using the string array and a default spinner layout
      ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this,
               R.array.planets_array, android.R.layout.simple_spinner_item);
      // Specify the layout to use when the list of choices appears
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      // Apply the adapter to the spinner
      spinner.setAdapter(adapter);
      if (savedInstanceState == null) {
          getSupportFragmentManager().beginTransaction()
                  .add(R.id.container, new PlaceholderFragment())
                  .commit();
      }
  }
  ...
  </code></pre>
  
  <p>fragment_main.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.spinners.MainActivity$PlaceholderFragment" &gt;
  
  &lt;Spinner
      android:id="@+id/planets_spinner"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>Strings.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;string name="app_name"&gt;Spinners&lt;/string&gt;    
  &lt;string name="action_settings"&gt;Settings&lt;/string&gt;
  &lt;string-array name="planets_array"&gt;
      &lt;item&gt;Mercury&lt;/item&gt;
      &lt;item&gt;Venus&lt;/item&gt;
      &lt;item&gt;Earth&lt;/item&gt;
      &lt;item&gt;Mars&lt;/item&gt;
      &lt;item&gt;Jupiter&lt;/item&gt;
      &lt;item&gt;Saturn&lt;/item&gt;
      &lt;item&gt;Uranus&lt;/item&gt;
      &lt;item&gt;Neptune&lt;/item&gt;
  &lt;/string-array&gt;
  </code></pre>
  
  <p></p>
  
  <p>When I try the app show some errors:</p>
  
  <pre><code>E/AndroidRuntime(1381): FATAL EXCEPTION: main
   Process: com.example.spinners, PID: 1381
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spinners/com.example.spinners.MainActivity}: java.lang.NullPointerException
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
      at android.app.ActivityThread.access$800(ActivityThread.java:135)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:136)
      at android.app.ActivityThread.main(ActivityThread.java:5017)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:515)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
      at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.NullPointerException
      at com.example.spinners.MainActivity.onCreate(MainActivity.java:28)
      at android.app.Activity.performCreate(Activity.java:5231)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
      ... 11 more
  </code></pre>
  
  <p>Line 28 is:</p>
  
  <pre><code>spinner.setAdapter(adapter);
  </code></pre>
  
  <p>Could you explain me why it's now working? Thank you!</p>