title: Number picker with Dynamic List
body: <p>I've been implementing the numpicker with Dynamic lists, i.e, that changes on some event, say it could be a button click or else. Problem is whenever I switch the List From Larger Length to Smaller length it works and smaller length to bigger length it ain't changes, While i always set 1st item as selected item, it crashes.</p>
  <p>Code:</p>
  <pre><code>public class MainActivity extends Activity {
      NumberPicker numPicker = null;
      String values1[]    =   null;
  
      String values2[]    =   null;
  
      String values3[]    =   null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          values1 =new String []{
                  &quot;Yello&quot;,
                  &quot;Red&quot;,
                  &quot;White&quot;,
                  &quot;Green&quot;,
                  &quot;Black&quot;
          };
          values2 =new String []{
                  &quot;Mango&quot;,
                  &quot;Orange&quot;
          };
          values3 =new String []{
                  &quot;Class&quot;,
                  &quot;Pointer&quot;,
                  &quot;Function&quot;,
                  &quot;Interafce&quot;,
                  &quot;Constructor&quot;
          };
  
  
  
          numPicker = (NumberPicker)findViewById(R.id.numberPicker1);
          numPicker.setDisplayedValues(values1);
  
          if(values1.length&gt;1)
          {
              numPicker.setMaxValue(values1.length-1);
          }
          else
          {
              numPicker.setMaxValue(values1.length);          
          }
          numPicker.setMinValue(0);   
          numPicker.setValue(0);
          numPicker.setBackgroundColor(1);
          numPicker.setWrapSelectorWheel(false);
          numPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
  
  
          //On value change listner for changing values
  
          /*      //For Tint Values
          numPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
              @Override
              public void onValueChange(NumberPicker numPic, int arg1, int arg2) {
                  numPickerNewVal = values[numPic.getValue()].trim();
  
                  tintTemp = getSelectedTintValue(numPickerNewVal);
  
  
              }
          });*/
  
  
  
      }
      public void onClick(View v)
      {
          switch(v.getId())
          {
          case R.id.button1:
              
              if(values1.length&gt;1)
              {
                  numPicker.setMaxValue(values1.length-1);
              }
              else
              {
                  numPicker.setMaxValue(values1.length);          
              }
              numPicker.setMinValue(0);   
              numPicker.setValue(0);
              numPicker.setDisplayedValues(values1);
              break;
          case R.id.button2:
              
              if(values2.length&gt;1)
              {
                  numPicker.setMaxValue(values2.length-1);
              }
              else
              {
                  numPicker.setMaxValue(values2.length);          
              }
              numPicker.setMinValue(0);   
              numPicker.setValue(0);
              numPicker.setDisplayedValues(values2);
              break;
          case R.id.button3:
              
              if(values3.length&gt;1)
              {
                  numPicker.setMaxValue(values3.length-1);
              }
              else
              {
                  numPicker.setMaxValue(values3.length);          
              }
              numPicker.setMinValue(0);   
              numPicker.setValue(0);
              numPicker.setDisplayedValues(values3);
              break;
          }
      }
  
  }
  </code></pre>
  <p>Logcat:</p>
  <pre><code>06-20 13:02:16.712: E/AndroidRuntime(16793): FATAL EXCEPTION: main
  06-20 13:02:16.712: E/AndroidRuntime(16793): java.lang.IllegalStateException: Could not execute method of the activity
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.view.View$1.onClick(View.java:3606)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.view.View.performClick(View.java:4211)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.view.View$PerformClick.run(View.java:17446)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.os.Handler.handleCallback(Handler.java:725)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.os.Handler.dispatchMessage(Handler.java:92)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.os.Looper.loop(Looper.java:153)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.app.ActivityThread.main(ActivityThread.java:5297)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at java.lang.reflect.Method.invokeNative(Native Method)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at java.lang.reflect.Method.invoke(Method.java:511)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at dalvik.system.NativeStart.main(Native Method)
  06-20 13:02:16.712: E/AndroidRuntime(16793): Caused by: java.lang.reflect.InvocationTargetException
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at java.lang.reflect.Method.invokeNative(Native Method)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at java.lang.reflect.Method.invoke(Method.java:511)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.view.View$1.onClick(View.java:3601)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    ... 11 more
  06-20 13:02:16.712: E/AndroidRuntime(16793): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=2; index=4
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.widget.NumberPicker.ensureCachedScrollSelectorValue(NumberPicker.java:1722)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.widget.NumberPicker.initializeSelectorWheelIndices(NumberPicker.java:1537)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at android.widget.NumberPicker.setMaxValue(NumberPicker.java:1345)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    at com.example.numpicker.MainActivity.onClick(MainActivity.java:85)
  06-20 13:02:16.712: E/AndroidRuntime(16793):    ... 14 more
  </code></pre>