Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to catch ArrayIndexOutOfBoundsException
    primarykey
    data
    text
    <p>I'm currently causing an <code>IndexOutOfBoundsException</code> that I can't seem to track down. I have a Spinner that is populated by a <code>String[]</code>. Depending on the manufacturer of the device, that <code>String[] (and hence Spinner object)</code> will have either 3 values or 1 value. </p> <p>I store the saved Spinner selection as an <code>int</code> in the <code>SharedPreferences</code> and load it up upon the <code>Activity</code> creation.</p> <p>In cases where the Spinner will only have 1 value, if it loads up the <code>SharedPreferences</code> and the saved value is <code>2</code>, I set that spinner to select item position 2, it will crash (obviously). I am trying to catch that exception so that I can then just set it to <code>0</code>, but I can't ever seem to catch that <code>Exception</code>. </p> <p>The code structure is as so:</p> <pre><code>Populate Spinner according to something Load up Saved Preferences Check some value of saved preferences try{ Set Spinner with that Preference } catch(Exception e) { Set Spinner with default value of 0 } </code></pre> <p>StackTrace:</p> <p><code>10-18 12:49:32.885: E/AndroidRuntime(15327): FATAL EXCEPTION: main 10-18 12:49:32.885: E/AndroidRuntime(15327): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 1 10-18 12:49:32.885: E/AndroidRuntime(15327): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 10-18 12:49:32.885: E/AndroidRuntime(15327): at java.util.ArrayList.get(ArrayList.java:304) 10-18 12:49:32.885: E/AndroidRuntime(15327): at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337) 10-18 12:49:32.885: E/AndroidRuntime(15327): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:390) </code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload