Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8351517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:39:16+00:00 2026-06-09T08:39:16+00:00

I have 3 Spinner dependent. But send me switch regions Exception THE FOLLOWING: Invalid

  • 0

I have 3 Spinner dependent. But send me switch regions Exception THE FOLLOWING:
Invalid IndexOutOfBoundException Location 0 Size is 0

The burden is always Spinner Region. The other two previous dependent.
Example: I select a region Show Cities. And if I select a city associated districts shows that City

The firs Time run ok but when i selectItem On Spinner Region the spinner Ciudad Launch an Exception

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nuevo_cliente);

    posicion=-1;
    txtnCliRut=(EditText) findViewById(R.id.txt_nuRut); 
    txtnCliNombre=(EditText) findViewById(R.id.txt_nuNombre);
    txtnCliDireccion=(EditText) findViewById(R.id.txt_nuDireccion);
    txtnCliTelefono=(EditText) findViewById(R.id.txt_nuTelefono);
    txtnCliRut.setKeyListener(DigitsKeyListener.getInstance("0123456789-kK"));
    txtnCliRut.addTextChangedListener(new InputValidator(txtnCliRut));
    txtnCliTelefono.setInputType(InputType.TYPE_CLASS_PHONE);
    btn_crear=(Button) findViewById(R.id.btnNuCliente);
    btn_cancelar=(Button) findViewById(R.id.btnNuCancela);

    //Declara Spinner
    spRegiones=(Spinner) findViewById(R.id.spRegion);
    spCiudades=(Spinner) findViewById(R.id.spCiudad);
    spComunas=(Spinner) findViewById(R.id.spComuna);
    //Declara Arreglos Objetos
    regiones= new ArrayList<Regiones>();
    ciudades =new ArrayList<Ciudades>();
    comunas =new ArrayList<Comunas>();
    Bundle extras = getIntent().getExtras();
    if (extras != null) 
    {
        posicion= extras.getInt("position");
        ClienteNuevo modCli= new ClienteNuevo();

        modCli=cargaDatos(posicion);
        txtnCliRut.setText(modCli.getRun_nCliente());
        txtnCliNombre.setText(modCli.getNombre_nCliente());
        txtnCliDireccion.setText(modCli.getDireccion_nCLiente());
        txtnCliTelefono.setText(modCli.getTelefono_nCliente());
        btn_crear.setText("Modificar");
    }

    String sFileSrc = null;     
    sFileSrc = "/PDAPedidos/SrcData/regiones.xml";
    oRegion = new ArrayList<Regiones>();
    oParseRegion = new CargaRegionesParserDom(sFileSrc);
    oRegion = oParseRegion.getARegiones();
    for(int i=0;i<oRegion.size();i++)
    {
        regiones.add(oRegion.get(i));
    }

    ArrayAdapter<Regiones> spRegionesAdapter= new ArrayAdapter<Regiones>(this, android.R.layout.simple_spinner_item, regiones);
    spRegionesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spRegiones.setAdapter(spRegionesAdapter);

    spRegiones.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id ) {

            ciudades.clear();
            comunas.clear();
            String sFileSrc = null;     
            sFileSrc = "/PDAPedidos/SrcData/ciudades.xml";
            oCiudad = new ArrayList<Ciudades>();
            oParseCiudad = new CargaCiudadesParserDom(sFileSrc);
            oCiudad = oParseCiudad.getACiudades();
            Log.i("Cargando-Regiones", "Posicion Select REGIOn "+position );
            int idRegion=0;
            idRegion=regiones.get(position).getCodigoRegion();
            for(int i=0;i<oCiudad.size();i++)
            {       
                if(idRegion==oCiudad.get(i).getCodigoRegionCiu())
                {
                    ciudades.add(oCiudad.get(i));
                }
            }

            Log.i("Cargando-Regiones", "Tamaño Arreglo Ciudades"+ ciudades.size());
            ArrayAdapter<Ciudades> spCiudadAdapter= new ArrayAdapter<Ciudades>(getApplicationContext(), android.R.layout.simple_spinner_item,ciudades);
            spCiudadAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spCiudades.setAdapter(spCiudadAdapter);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
        }
    });

    spCiudades.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id ) 
        {
            comunas.clear();
            String sFileSrc = null;     
            sFileSrc = "/PDAPedidos/SrcData/comunas.xml";
            oComuna = new ArrayList<Comunas>();
            oParseComuna = new CargaComunasParserDom(sFileSrc);
            oComuna = oParseComuna.getAComunas();

            int idCiudad=0;
            Log.i("Cargando-Regiones", "id Ciudad "+position );
            idCiudad=ciudades.get(position).getCodigoCiudad();       
            Log.i("Cargando-Regiones", "id Ciudad "+idCiudad );
            for(int i=0;i<oComuna.size();i++)
            {
                if(idCiudad==oComuna.get(i).getCodigoCiudadCom())
                {
                    comunas.add(oComuna.get(i));
                }
            }

            ArrayAdapter<Comunas> spComunaAdapter= new ArrayAdapter<Comunas>(getApplicationContext(), android.R.layout.simple_spinner_item, comunas);
            spComunaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            spComunas.setAdapter(spComunaAdapter); 
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    });

    final ClienteNuevo oNewCli= new ClienteNuevo();
    btn_crear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            oNewCli.setRun_nCliente(txtnCliRut.getText().toString().trim());
            oNewCli.setNombre_nCliente(txtnCliNombre.getText().toString().trim());
            oNewCli.setDireccion_nCLiente(txtnCliDireccion.getText().toString().trim());
            oNewCli.setCiudad_nCliente(txtnCliCiudad.getText().toString().trim());
            oNewCli.setComuna_nCliente(txtnCliComuna.getText().toString().trim());
            oNewCli.setTelefono_nCliente(txtnCliTelefono.getText().toString().trim());
            if(posicion>=0)
            {
                modificaCli(oNewCli,posicion);
                finish();
            }
            else
            {
                creaNuevoCliente(oNewCli);
                finish();
            }
        }
    });
}

Logcat:

08-08 17:01:11.643: E/AndroidRuntime(18992): Uncaught handler: thread main exiting due to uncaught exception
08-08 17:01:11.673: E/AndroidRuntime(18992): java.lang.IndexOutOfBoundsException: Invalid location 0, size is 0
08-08 17:01:11.673: E/AndroidRuntime(18992):    at java.util.ArrayList.get(ArrayList.java:341)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:351)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:198)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3026)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:909)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.ScrollView.onMeasure(ScrollView.java:283)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3026)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:453)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.TableLayout.measureVertical(TableLayout.java:465)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.TableLayout.onMeasure(TableLayout.java:428)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3026)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3026)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.View.measure(View.java:8018)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.os.Looper.loop(Looper.java:123)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at android.app.ActivityThread.main(ActivityThread.java:4363)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at java.lang.reflect.Method.invokeNative(Native Method)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at java.lang.reflect.Method.invoke(Method.java:521)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-08 17:01:11.673: E/AndroidRuntime(18992):    at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:39:18+00:00Added an answer on June 9, 2026 at 8:39 am

    I’m guessing that ciudades is empty (has size == 0) because of this:

    if(idRegion==oCiudad.get(i).getCodigoRegionCiu())
    

    I looks like you are trying to compare two Regiones data types. Understand that in Java you cannot use == for custom classes. You need to use equals(). Consider this trivial example:

    class Foo {
        String bar;
    
        Foo(String string) {
            this.bar = string;
        }
    
        @Override
        public boolean equals(Object o) {
            if(o instanceof Foo) // check 1
                return bar.equals(((Foo) o).bar);
            else if(o instanceof String) // check 2
                return bar.equals(o);
            return false; // else
        }
    }
    

    Now if I ask:

    Foo foo = new Foo("A");
    Foo foo2 = new Foo("A");
    
    foo == foo2;              // false, cannot use == operator for custom classes
    foo.equals(new Foo("a")); // false, check 1 says "A" is not "a"
    foo.equals(foo2);         // true, check 1
    foo.equals("A");          // true, check 2
    foo.equals(1);            // false, else
    

    Two other very useful methods to override are toString() and hashCode().

    • toString() defines a user readable representation of your class, I’m guessing you already did this or your Spinners have some very ugly Strings in them.

    • hashCode() is used by many built-in data types. For example if you wanted to sort a Foo array, this is the first method Java tries to use.

    Here are Foo’s versions of these methods:

    @Override
    public int hashCode() {
        return bar.hashCode();
    }
    
    @Override
    public String toString() {
        return bar;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a spinner with data content, but the spinner is very small. How
I have a custom spinner layout with an image and a text view but
I have spinner with array list thats work fine, but i want to sort
I have spinner which I populate from SQLite database. But I want to populate
I have spinner and code android.R.layout.simple_spinner_dropdown_item . But I get weird height like this:
I have a Spinner.onItemSelected() method and I want to be able to have an
I have a spinner witch is populated from database, when I select an item
I have one spinner, and I am populating that. I have list of values
I have a spinner and I am trying to populate it manually in the
I have Two spinner(Subject and Chapter),I crate two table(Chapter,subject). I add all subject name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.