title: Spinner causes null pointer exception
body: <p>I have tried all suggestions in similar questions but I can't fix it. A Spinner view is giving me a null pointer exception. The exception occurs at the following line: <code>spinner.setAdapter(adapter);</code>. I suspect it has something to do with it not being inflated correctly. The String array should not be the problem. I have also tried an almost exact copy of the code here: <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>I have also cleaned my code multiple times. Can anyone find the problem?</p>
  
  <p>Code:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_settings);
  
          Spinner spinner = (Spinner) findViewById(R.id.themeSpinner);
          String[] themesArray = new String[]{"Dark Holo Theme"};
  
  
          // Create an ArrayAdapter using the string array and a default spinner layout
          ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, themesArray);
          // 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);
  </code></pre>
  
  <p>XML:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/relativeLayout1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".SettingsActivity" &gt;
  
      &lt;Spinner
          android:id="@+id/themeSpinner"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/screenDimSwitch"
          android:layout_centerHorizontal="true" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Logcat:</p>
  
  <blockquote>
    <p>12-28 16:22:04.011: E/AndroidRuntime(12269): FATAL EXCEPTION: main
    12-28 16:22:04.011: E/AndroidRuntime(12269):
    java.lang.RuntimeException: Unable to start activity
    ComponentInfo{com.example.sudokuandroidprobeersel/com.example.sudokuandroidprobeersel.SettingsActivity}:
    java.lang.NullPointerException 12-28 16:22:04.011:
    E/AndroidRuntime(12269):  at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
    12-28 16:22:04.011: E/AndroidRuntime(12269):  at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
    12-28 16:22:04.011: E/AndroidRuntime(12269):  at
    android.app.ActivityThread.access$600(ActivityThread.java:127) 12-28
    16:22:04.011: E/AndroidRuntime(12269):    at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
    12-28 16:22:04.011: E/AndroidRuntime(12269):  at
    android.os.Handler.dispatchMessage(Handler.java:99) 12-28
    16:22:04.011: E/AndroidRuntime(12269):    at
    android.os.Looper.loop(Looper.java:137) 12-28 16:22:04.011:
    E/AndroidRuntime(12269):  at
    android.app.ActivityThread.main(ActivityThread.java:4512) 12-28
    16:22:04.011: E/AndroidRuntime(12269):    at
    java.lang.reflect.Method.invokeNative(Native Method) 12-28
    16:22:04.011: E/AndroidRuntime(12269):    at
    java.lang.reflect.Method.invoke(Method.java:511) 12-28 16:22:04.011:
    E/AndroidRuntime(12269):  at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984)
    12-28 16:22:04.011: E/AndroidRuntime(12269):  at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751) 12-28
    16:22:04.011: E/AndroidRuntime(12269):    at
    dalvik.system.NativeStart.main(Native Method) 12-28 16:22:04.011:
    E/AndroidRuntime(12269): Caused by: java.lang.NullPointerException
    12-28 16:22:04.011: E/AndroidRuntime(12269):  at
    com.example.sudokuandroidprobeersel.SettingsActivity.onCreate(SettingsActivity.java:80)
    12-28 16:22:04.011: E/AndroidRuntime(12269):  at
    android.app.Activity.performCreate(Activity.java:4465) 12-28
    16:22:04.011: E/AndroidRuntime(12269):    at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
    12-28 16:22:04.011: E/AndroidRuntime(12269):  at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
    12-28 16:22:04.011: E/AndroidRuntime(12269):  ... 11 more</p>
  </blockquote>
  
  <p><strong>Update:</strong></p>
  
  <p>The object that causes the null pointer exception is the Spinner, not the adapter. R.id.themeSpinner is correctly referenced in R though.</p>