Android Studio error: java.lang.ArrayIndexOutOfBoundsException: length = 1; index = 1

1

I am having a problem that has been going on for hours and I can not solve it. The error that gives me when compiling the program is the title.

This is my class:

public class ElegirCuidados extends AppCompatActivity {

private String elegirCuidados;
String[] miArray = {};
ArrayAdapter<String> arrayAdapter;
ListView lvDias;
String dia = "6";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.elegir_cuidados);

    miArray = new String[]{dia};
    lvDias = findViewById(R.id.lvDias);

    for(int i = 0; i < Integer.parseInt(dia); i++){
        miArray[i] = ("Día "+ i);
    }

    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, miArray);
    lvDias.setAdapter(arrayAdapter);
}

}

This is the Logcat:

  

11-14 12: 30: 09.936 15663-15663 / com.example.rodrigo.petshotel E / AndroidRuntime: FATAL EXCEPTION: main       Process: com.example.rodrigo.petshotel, PID: 15663       java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.rodrigo.petshotel / com.example.rodrigo.petshotel.ChooseCoverage}: java.lang.ArrayIndexOutOfBoundsException: length = 1; index = 1           at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2678)           at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2743)           at android.app.ActivityThread.-wrap12 (ActivityThread.java)           at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1490)           at android.os.Handler.dispatchMessage (Handler.java:102)           at android.os.Looper.loop (Looper.java:154)           at android.app.ActivityThread.main (ActivityThread.java:6165)           at java.lang.reflect.Method.invoke (Native Method)           at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:888)           at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:778)        Caused by: java.lang.ArrayIndexOutOfBoundsException: length = 1; index = 1           at com.example.rodrigo.petshotel.SelectCare.onCreate (ChooseCare.java:31)           at android.app.Activity.performCreate (Activity.java:6687)           at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1140)           at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2631)           at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2743)           at android.app.ActivityThread.-wrap12 (ActivityThread.java)           at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1490)           at android.os.Handler.dispatchMessage (Handler.java:102)           at android.os.Looper.loop (Looper.java:154)           at android.app.ActivityThread.main (ActivityThread.java:6165)           at java.lang.reflect.Method.invoke (Native Method)           at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:888)           at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:778)

I really do not understand where the error may be, I tried everything now. Could you help me?

The result of this would be to show these elements in the ListView: Day 1 Day 2 Day 3 Day 4 Day 5 Day 6

Thank you!

    
asked by Rodrigo 14.11.2018 в 16:39
source

1 answer

0

You are trying to read a second element of your array (index = 1, remember to start from 0), when your array contains only one element (length = 1)

  

java.lang.ArrayIndexOutOfBoundsException: length = 1; index = 1

I suggest you define the maximum length in your loop as the length of the array ( i < miArray.length ):

//   for(int i = 0; i < Integer.parseInt(dia); i++){
   for(int i = 0; i < miArray.length; i++){
        miArray[i] = ("Día "+ i);
    }

This way you will only enter values to your array depending on its length.

To show the 6 elements that you want to display in your Adapter, you must add 6 elements to the array:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.elegir_cuidados);

    //*Agrega elementos a array
    miArray = new String[]{"1","2","3","4","5","6"};

    lvDias = findViewById(R.id.lvDias);

    //for(int i = 0; i < Integer.parseInt(dia); i++){
    for(int i = 0; i < miArray.length; i++){
        miArray[i] = ("Día "+ i);
    }

    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, miArray);
    lvDias.setAdapter(arrayAdapter);
}
    
answered by 14.11.2018 / 16:53
source