0

I want to create 72 Buttons, each with a specific callback. Now, I'd like to set these callbacks using a loop, instead of having to write hundreds of lines of code.

buttons = new ImageButton[72];
        for (int j = 1; j <= 72; j++) {

            String buttonID = "btn"+ j;
            int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
            buttons[j] = findViewById(resID);
            buttons[j].setOnClickListener(this);

        }

Now I get this error:

Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 

greatly appreciate any help.

LX_12221
  • 1
  • 1

0 Answers0