When the third Spinner data is selected, the Activity corresponding to the Spinner is closed

1

The window where the Spinner is located is closed when I select the third option of the Spinner, with the first and second there are no problems ...

This is the error in the Logcat

05-03 00:57:29.436 11301-11301/? E/AndroidRuntime:   
FATAL EXCEPTION: main Process:  com.example.matias.finalcode, PID: 11301
   java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
       at com.example.matias.finalcode.SignupActivityEmpresa.onItemSelected(SignupActivityEmpresa.java:199)
       at android.widget.AdapterView.fireOnSelected(AdapterView.java:897)
       at android.widget.AdapterView.access$200(AdapterView.java:48)
       at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:865)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5258)
       at java.lang.reflect.Method.invoke(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
05-03 00:57:29.437 788-1622/? W/ActivityManager:   Force finishing activity com.example.matias.finalcode/.SignupActivityEmpresa

And this is the code referenced in the error

@Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        int[] localidades = {R.array.array_sevilla, R.array.array_malaga};

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this,
                localidades[i],
                android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerLoc.setAdapter(adapter);

        provincia = adapterView.getItemAtPosition(i).toString();

    }

The arrays in the string.xml file are the following

<string-array name="array_provincias">
        <item>Selecciona un país</item>
        <item>Sevilla</item>
        <item>Malaga</item>
    </string-array>

    <string-array name="array_sevilla">
        <item>Selecciona una ciudad</item>
        <item>Tomares</item>
        <item>Camas</item>
    </string-array>

    <string-array name="array_malaga">
        <item>Selecciona una ciudad</item>
        <item>Casares</item>
        <item>Estepona</item>
    </string-array>

From here I get the value of [i] by passing it to onItemSelected ...

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this,
                R.array.array_provincias,
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerPro.setAdapter(adapter);
        spinnerPro.setOnItemSelectedListener(this);

What can it be? I do not know what the error is.

Thanks for your time!

    
asked by Matías Nicolás Núñez Rivas 03.05.2018 в 15:07
source

1 answer

4

I recommend debugging and verifying the value of the parameter [i] since the error jumps, you are passing the value 2 and the array is 2 positions. [0] y [1]

A rough way something you could do, is to subtract the [i] from a unit when you receive the parameter.

@Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    i = i-1;//Reducimos en 1 el valor que tiene i para evitar el error
    int[] localidades = {R.array.array_sevilla, R.array.array_malaga};

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this,
            localidades[i],
            android.R.layout.simple_spinner_item);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerLoc.setAdapter(adapter);

    provincia = adapterView.getItemAtPosition(i).toString();

}

With that you should solve your problem.

    
answered by 03.05.2018 в 15:22