Order of OnItemSelectedListener Calls After OnCreate
<p>I have two spinners in an Activity where the second Spinner's selection set is based on what the user picked for the first Spinner.  I use a private class variable in the Activity which is set in the top Spinner's OnItemSelectedListener and then referenced in the bottom Spinner's OnItemSelectedListener to obtain the correct selection set.</p>

<p>This almost always works, but sometimes (mainly when app was run, not exited, and then started again by a user click some long time later) I get a null pointer exception in the second Spinner's OnItemSelectedListener due to this local variable not being set.  This indicates to me that after the OnCreate that the second Spinner's OnItemSelectedListener was called before the first Spinner's.  </p>

<p>Is there any method to force a certain order in the listeners being fired or is there a better design approach to handle this second Spinner's dependency on the first Spinner?</p>

<p>Example code:
    package com.crashtestdummylimited.navydecoder;    </p>

<pre><code>public class Test extends Activity {    

  // Variable that at times is still null
  private ReferenceData     referenceData;    

  private void setupSpinnerFromArray (int spinnerId, String stringArray[], OnItemSelectedListener listener) {    
    Spinner spinner = (Spinner) findViewById(spinnerId);    

    ArrayAdapter &lt;CharSequence&gt; adapter = new ArrayAdapter &lt;CharSequence&gt;(    
      this, android.R.layout.simple_spinner_item, stringArray);    

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);    

    spinner.setAdapter(adapter);    

    spinner.setOnItemSelectedListener(listener);    
  }    

  @Override    
  public void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);    

    setContentView(R.layout.main_screen);    

    // Setup Top (main) Spinner    
    Spinner spinner1 = (Spinner) findViewById(R.id.mainDecodeSpinner);    

    ArrayAdapter&lt;CharSequence&gt; adapter1 = ArrayAdapter.createFromResource(    
      this, R.array.level0_list_array, android.R.layout.simple_spinner_item);    

    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);    
    spinner1.setAdapter(adapter1);    

    spinner1.setOnItemSelectedListener(new MainDecoderItemSelectedListener());    


    // Setup Bottom (dependent) Spinner    
    setupSpinnerFromArray(R.id.secondaryDecodeSpinner, R.array.level1_list_array, new SecondaryDecoderItemSelectedListener());    
   }    



  public class MainDecoderItemSelectedListener implements OnItemSelectedListener {    

    public void onItemSelected(AdapterView&lt;?&gt; parent,    
                                View view, int pos, long id) {    



      String selectedString = parent.getItemAtPosition(pos).toString();    

      if (selectedString.equals("AAA")){    
        // Problem variable is set
        referenceData = new RatingCodes();    
        setupSpinnerFromArray(R.id.secondaryDecodeSpinner, referenceData.getKeys(), new SecondaryDecoderItemSelectedListener());    
      }          
      else if (selectedString.equals("BBB")){    
        // Problem variable is set
        referenceData = new IMSCodes();      
        setupSpinnerFromArray(R.id.secondaryDecodeSpinner, referenceData.getKeys(), new SecondaryDecoderItemSelectedListener());    
      }    
      //  TODO: Improve what occurs if no match which should not occur    
    }    

    public void onNothingSelected(AdapterView&lt;?&gt; parent) {    
      // Do nothing.    
    }    
  }    


  public class SecondaryDecoderItemSelectedListener implements OnItemSelectedListener {    

    public void onItemSelected(AdapterView&lt;?&gt; parent,    
                                View view, int pos, long id) {    

      String key = parent.getItemAtPosition(pos).toString();    

      // **** referenceData being null at this point has caused crashed ****    
      String value = referenceData.getValue(key);    

      // ...  Update text on activity screen  ...    
    }    

    public void onNothingSelected(AdapterView&lt;?&gt; parent) {    
      // Do nothing.    
    }    
  }    

}    
</code></pre>
