Array of Buttons in Android

后端 未结 6 1892
庸人自扰
庸人自扰 2020-12-29 12:34

I want to map the buttons to an array of buttons and the code has no errors while compiling but there is force close when i run it:

Button buttons[];

@Overr         


        
6条回答
  •  情话喂你
    2020-12-29 12:51

    Try the following code:

    private int objectLength = 9; //Array elements 
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_board_view);
    
        Button[] buttons = new Button[objectLength]; 
        buttons[0] = (Button)findViewById(R.id.buttonOne);
        buttons[1] = (Button)findViewById(R.id.buttonTwo);
        buttons[2] = (Button)findViewById(R.id.buttonThree);
        buttons[3] = (Button)findViewById(R.id.buttonFour);
        buttons[4] = (Button)findViewById(R.id.buttonFive);
        buttons[5] = (Button)findViewById(R.id.buttonSix);
        buttons[6] = (Button)findViewById(R.id.buttonSeven);
        buttons[7] = (Button)findViewById(R.id.buttonEight);
        buttons[8] = (Button)findViewById(R.id.buttonMid);
    }
    

提交回复
热议问题