-1

In android, I added onClickListener to all buttons.

The codes:

        ArrayList<ImageButton> alfabebutonlar = new ArrayList<ImageButton>();

        alfabebutonlar.add((ImageButton)findViewById(R.id.harfab));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfbb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfcb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfccb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfdb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfeb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harffb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfgb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfggb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfhb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfib));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfiib));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfjb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfkb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harflb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfmb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfnb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfob));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfoob));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfpb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfrb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfsb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfssb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harftb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfub));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfuub));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfvb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfyb));
        alfabebutonlar.add((ImageButton)findViewById(R.id.harfzb));

        ArrayList<String> alfabeharfler = new ArrayList<String>();

        alfabeharfler.add("A");
        alfabeharfler.add("B");
        alfabeharfler.add("C");
        alfabeharfler.add("Ç");
        alfabeharfler.add("D");
        alfabeharfler.add("E");
        alfabeharfler.add("F");
        alfabeharfler.add("G");
        alfabeharfler.add("Ğ");
        alfabeharfler.add("H");
        alfabeharfler.add("İ");
        alfabeharfler.add("I");
        alfabeharfler.add("J");
        alfabeharfler.add("K");
        alfabeharfler.add("L");
        alfabeharfler.add("M");
        alfabeharfler.add("N");
        alfabeharfler.add("O");
        alfabeharfler.add("Ö");
        alfabeharfler.add("P");
        alfabeharfler.add("R");
        alfabeharfler.add("S");
        alfabeharfler.add("Ş");
        alfabeharfler.add("T");
        alfabeharfler.add("U");
        alfabeharfler.add("Ü");
        alfabeharfler.add("V");
        alfabeharfler.add("Y");
        alfabeharfler.add("Z");

        for(i = 0; i < alfabebutonlar.size(); i++){

            alfabebutonlar.get(i).setOnClickListener ( new View.OnClickListener () {
                public void onClick(View alfabebutonv){

                    startActivity(getIntent().putExtra("harf", alfabeharfler.get(i)));

                }
            });

        }

But when I click in button B I get this error (log):

java.lang.IndexOutOfBoundsException: Invalid index 29, size is 29

The logs:

java.lang.IndexOutOfBoundsException: Invalid index 29, size is 29

E/AndroidRuntime( 4367):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

E/AndroidRuntime( 4367):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)

E/AndroidRuntime( 4367):    at android.app.ActivityThread.access$800(ActivityThread.java:151)

E/AndroidRuntime( 4367):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)

E/AndroidRuntime( 4367):    at android.os.Handler.dispatchMessage(Handler.java:102)

E/AndroidRuntime( 4367):    at android.os.Looper.loop(Looper.java:135)

E/AndroidRuntime( 4367):    at android.app.ActivityThread.main(ActivityThread.java:5254)

E/AndroidRuntime( 4367):    at java.lang.reflect.Method.invoke(Native Method)

E/AndroidRuntime( 4367):    at java.lang.reflect.Method.invoke(Method.java:372)

E/AndroidRuntime( 4367):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

E/AndroidRuntime( 4367):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

E/AndroidRuntime( 4367): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 29, size is 29

E/AndroidRuntime( 4367):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)

E/AndroidRuntime( 4367):    at java.util.ArrayList.get(ArrayList.java:308)

E/AndroidRuntime( 4367):    at com.example.xxx.xxx.alfabe.onCreate(alfabe.java:241)

E/AndroidRuntime( 4367):    at android.app.Activity.performCreate(Activity.java:5990)

E/AndroidRuntime( 4367):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)

E/AndroidRuntime( 4367):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)

E/AndroidRuntime( 4367):    ... 10 more

I added onClickListener to all buttons in for loop for saving code. I think I get an error because in for loop.

EDIT:

I added this line in loop: final int index = i; I writed index to i. And the problem was resolved.

I hope you understand.

  • Which line did you get the error? Also, could you post the whole crash log? – Bach Vu Jul 25 '19 at 03:35
  • @BachVu I get the error in the for loopb Ithink. I will edited the question – Ahmet Veli Jul 25 '19 at 03:38
  • @BachVu I edited question and added logs. – Ahmet Veli Jul 25 '19 at 03:40
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – ADM Jul 25 '19 at 03:46
  • Do not access i inside the listener. When the for loop is run i is incremented upto 29 (28 elements) and exited. At this time listener is not executed. When you click a button i is accessed with value 29 and hence the error. Create a final map between button and label outside the for loop and aceess it from the listener. – fiveelements Jul 25 '19 at 03:57

5 Answers5

0

Just make sure that both arrays (alfabeharfler and alfabebutonlar) and have the same number of elements. Or event better, if both arrays need to match size and position, use a Pair object instead...

ArrayList<Pair<String, ImageButton>> list = new ArrayList<Pair<String, ImageButton>>();
list.add(new Pair("A", imageButton));

To access the a specific item...

list.get(i).first; //The letter/string
list.get(i).second; //The button
Leo
  • 14,625
  • 2
  • 37
  • 55
0

Do this in your OnClick :

            //ButtonView arraylist and data arraylist size should be same....
            public void onClick(View alfabebutonv){
                 final int index = i;
                 // Ith position of buttonview should be less than the data arraylist size
                 // Add below check. i < alfabeharfler.size()
                 if( alfabeharfler != null && i < alfabeharfler.size()) { 
                    startActivity(getIntent().putExtra("harf", alfabeharfler.get(index)));
                 }

            }
        });

    }
RajeshVijayakumar
  • 10,281
  • 11
  • 57
  • 84
0

Try this:

for(i = 0; i < alfabebutonlar.size(); i++){
            String value = alfabeharfler.get(i);

            alfabebutonlar.get(i).setOnClickListener ( new View.OnClickListener () {
                public void onClick(View alfabebutonv){

                    startActivity(getIntent().putExtra("harf", value));
                }
            });

        }
Bach Vu
  • 2,298
  • 1
  • 15
  • 19
0

Inside your method onClick(), it has a reference to your "i" variable, and its last value is 29, which is when it breaks from your loop, so when you call alfabeharfler.get(i), its using the value 29 in all your calls, not just when you click on button B, and because of this you got that exception "IndexOutOfBoundsException".

Try declaring a new final variable inside your loop and use that inside your onClick method, something like this (I haven't tried this code, but it should work):

for(i = 0; i < alfabebutonlar.size(); i++){
    final int index = i;
    alfabebutonlar.get(index).setOnClickListener ( new View.OnClickListener () {
        public void onClick(View alfabebutonv){

            startActivity(getIntent().putExtra("harf", alfabeharfler.get(index)));

        }
    });

}

Hope this helps.

Alejandro
  • 325
  • 4
  • 10
0

Do not access i inside the listener. When the for loop is run i is incremented upto 29 (28 elements) and exited. At this time listener is not executed. When you click a button i is accessed with value 29 and hence the error. Create a final map between button and label outside the for loop and aceess it from the listener to retrieve and use the label value.

fiveelements
  • 3,649
  • 1
  • 17
  • 16