I am makin a tic tac toe game. My circle image does not initialize and shows out of memory error.
I tried using other image but it does not work.
My java code
JavaScript
x
btn1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
btn1.setEnabled( false );
if (chance==0){
box1=0;
chance=1;
btn1.setBackgroundResource( R.drawable.cross );
}else{
box1=1;
chance=0;
btn1.setBackgroundResource( R.drawable.circle );
}
}
} );
btn2.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
btn2.setEnabled( false );
if (chance==0){
box2=0;
chance=1;
btn2.setBackgroundResource( R.drawable.cross );
}else{
box2=1;
chance=0;
btn2.setBackgroundResource( R.drawable.circle );
}
}
} );
and so on for 9 buttons
Thanks in advance and hope this question is clear!.
Advertisement
Answer
Solution:
change the configuration on the virtual devices in the emulator under “Emulated Performance – Graphics” so they use Software GLES 1.1
Note: Try to make your code easier to read. Idea:
Don’t use variables box1, box2 …, make an
int[] box = new int[9];
Use a class that
extends OnClickListener
Code:
JavaScript
private class BoxClickListener implements View.OnClickListener {
private final int index;
public BoxClickListener(int index) {
this.index = index;
}
@Override
public void onClick(View v) {
v.setEnabled(false);
if (chance == 0) {
box[index] = 0;
chance = 1;
v.setBackgroundResource(R.drawable.cross);
} else {
box[index] = 1;
chance = 0;
v.setBackgroundResource(R.drawable.circle);
}
}
}
How to apply it:
JavaScript
btn1.setOnClickListener(new BoxClickListener(1));
btn2.setOnClickListener(new BoxClickListener(2));