NumberPicker serviceWheel = (NumberPicker) findViewById(R.id.serviceSelector);
serviceWheel.setMaxValue(serviceArray.length());
String[] profiles = new String[serviceArray.length()];
for (int i = 0; i < serviceArray.length(); i++){
JSONObject profileObject = serviceArray.getJSONObject(i);
profiles[i] = profileObject.getString("description");
}
serviceWheel.setDisplayedValues(profiles);
serviceWheel.setWrapSelectorWheel(false);
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at android.widget.NumberPicker.ensureCachedScrollSelectorValue(NumberPicker.java:1756)
at android.widget.NumberPicker.incrementSelectorIndices(NumberPicker.java:1722)
at android.widget.NumberPicker.scrollBy(NumberPicker.java:1097)
at android.widget.NumberPicker.onTouchEvent(NumberPicker.java:880)
at android.view.View.dispatchTouchEvent(View.java:7565)
serviceArray
is a JSONArray
, with a length of 3
. I tried to add padding when instatiating the array ( serviceArray.length() + 1
) but this caused failure before the activity was shown. The NumberPicker displays fine but when i scroll to the last entry of the array, it crashes with this error. There is no reference to code line in my code
Change from this:
serviceWheel.setMaxValue(serviceArray.length());
to this:
serviceWheel.setMaxValue(serviceArray.length() - 1);