Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.lang.IndexOutOfBoundsException: Invalid index x, size is y
    text
    copied!<p>I have an activity where there are 2 spinners. I fill the spinners with lists taken from an xml file like this one :</p> <pre><code>&lt;outil id="1" name="Décompacteur"&gt; &lt;travail id="1"&gt;Travail au sol&lt;/travail&gt; &lt;travail id="1001"&gt;Autre&lt;/travail&gt; &lt;/outil&gt; &lt;outil id="2" name="Cultivateur"&gt; &lt;travail id="3"&gt;Travail au sol&lt;/travail&gt; &lt;travail id="1002"&gt;Autre&lt;/travail&gt; &lt;/outil&gt; </code></pre> <p>In another activity, I can change the file from which I build the lists.</p> <p>My problem is : I select a tool (outil) at the end of a long list of tools, for example position = 6. Then I change the file, and the tool list now has only 4 items. I set a new adapter :</p> <pre><code>listNameOutil = getOutilNameList(listOutilObject); adapterOutil = new ArrayAdapter&lt;String&gt;(this, R.layout.spinners_base, listNameOutil); adapterOutil.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner_outil.setAdapter(adapterOutil); </code></pre> <p>But I still get an error, and I don't know where it comes from provided there is no "caused by" in the stack trace... Please note that I also do a <code>spinner_outil.setSelection(positionSaved);</code>. At first, I was trying to catch the exception on this line but it seems that it's not this line which is the problem.</p> <p>Any ideas to solve this?</p> <p>Here is the Stack trace :</p> <pre><code>06-27 14:46:01.811: W/dalvikvm(20824): threadid=1: thread exiting with uncaught exception (group=0x40d0c930) 06-27 14:46:01.821: E/AndroidRuntime(20824): FATAL EXCEPTION: main 06-27 14:46:01.821: E/AndroidRuntime(20824): java.lang.IndexOutOfBoundsException: Invalid index 6, size is 4 06-27 14:46:01.821: E/AndroidRuntime(20824): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 06-27 14:46:01.821: E/AndroidRuntime(20824): at java.util.ArrayList.get(ArrayList.java:304) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:390) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.Spinner.makeAndAddView(Spinner.java:546) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.Spinner.layout(Spinner.java:495) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.Spinner.onLayout(Spinner.java:459) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.View.layout(View.java:14008) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewGroup.layout(ViewGroup.java:4373) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.View.layout(View.java:14008) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewGroup.layout(ViewGroup.java:4373) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.View.layout(View.java:14008) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewGroup.layout(ViewGroup.java:4373) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.View.layout(View.java:14008) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewGroup.layout(ViewGroup.java:4373) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.View.layout(View.java:14008) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewGroup.layout(ViewGroup.java:4373) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.Choreographer.doFrame(Choreographer.java:532) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.os.Handler.handleCallback(Handler.java:725) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.os.Handler.dispatchMessage(Handler.java:92) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.os.Looper.loop(Looper.java:137) 06-27 14:46:01.821: E/AndroidRuntime(20824): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-27 14:46:01.821: E/AndroidRuntime(20824): at java.lang.reflect.Method.invokeNative(Native Method) 06-27 14:46:01.821: E/AndroidRuntime(20824): at java.lang.reflect.Method.invoke(Method.java:511) 06-27 14:46:01.821: E/AndroidRuntime(20824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-27 14:46:01.821: E/AndroidRuntime(20824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-27 14:46:01.821: E/AndroidRuntime(20824): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p><strong>EDIT :</strong></p> <p>In the on Start, I bind to my service. Then, in onServiceConnected, I ask the service for configuration informations :</p> <pre><code> try { remoteServiceMain.requeteLireConfig(); } catch (RemoteException e) { e.printStackTrace(); } </code></pre> <p>Then, still in the onServiceConnected, I define the layout :</p> <pre><code>spinner_outil = (Spinner) findViewById(R.id.spinner_outil_layout); spinner_outil.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int position, long arg3) { } } </code></pre> <p>Then onServiceConnected is finished. Please note that I get the last log before error there.</p> <p><strong>REDDIT :</strong></p> <p>I do <code>setSelection(positionSaved);</code> after <code>spinner_outil.setAdapter(adapterOutil);</code>, and I set positionSaved when I get the answer from the service after calling :</p> <pre><code> try { remoteServiceMain.requeteLireConfig(); } catch (RemoteException e) { e.printStackTrace(); } </code></pre> <p><code>positionSaved</code> is sent by the Service, which takes it from a Configuration File. <code>positionSaved</code> is saved evrytime the user clicks on an Item of the spinner. The reason why I get it from the service and not directly from the File is that I use the same method for other things</p>
 

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