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
I had situation like this, I chose different approach. I stored id into integer array.
Int[] btnarr = new int[3];
btn[0]=R.id.button1; // give your ID
Button btn = (Button).findViewById(cc[i]);
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);
}
Button buttons[] = null;
button has to be created, using the new
operator:
Button buttons[] = new Button[9];
It's usually better if you don't have to hardcode constants like a 9 into your code. And you usually don't need to.
You can for example put the ids into an array and build a dynamically sized List
based on them
private List<Button> buttons;
private static final int[] BUTTON_IDS = {
R.id.buttonOne,
R.id.buttonTwo,
R.id.buttonThree,
R.id.buttonFour,
R.id.buttonFive,
R.id.buttonSix,
R.id.buttonSeven,
R.id.buttonEight,
R.id.buttonMid,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_board_view);
buttons = new ArrayList<Button>();
// or slightly better
// buttons = new ArrayList<Button>(BUTTON_IDS.length);
for(int id : BUTTON_IDS) {
Button button = (Button)findViewById(id);
button.setOnClickListener(this); // maybe
buttons.add(button);
}
}
Your array is null and you're trying to get an index into it. That is what's causing the NullPointerException
. Your array must be initialized before you can use it to store your buttons.
If you want an array of nine buttons then change this line:
Button buttons[] = null;
To this:
Button buttons[] = new Button[9];
Also, you have a class member Button buttons[]
and a local function variable that is also named Button buttons[]
. If this is intentional then by all means carry on. Otherwise, you'll want to further change your line to this:
buttons[] = new Button[9];
EXAMPLE USAGE:
Button[] buttons = initializeButtons(3);
buttons[1].setText("I am button1");
buttons[2].setText("I am button2");
buttons[3].setText("I am button3");
FUNCTION:
public Button[] initializeButtons(int x) {
Resources res = getResources();
Button[] buttons = new Button[x];
for (int i = 0; i < x; i++) {
String b = "button" + i;
buttons[i] = (Button) findViewById(res.getIdentifier(b, "id", getPackageName()));
} return buttons;//to prevent array out of bounds exception start from 0
}
NOTE: Make sure in your layout the button id is button1, button2, button3, .. .. etc'