Skip to content
Advertisement

Image not being initialized

I am makin a tic tac toe game. My circle image does not initialize and shows out of memory error.

It shows this in Run: Click here for image

I tried using other image but it does not work.

My java code

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

Source


Note: Try to make your code easier to read. Idea:

  1. Don’t use variables box1, box2 …, make an int[] box = new int[9];

  2. Use a class that extends OnClickListener

Code:

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:

btn1.setOnClickListener(new BoxClickListener(1));
btn2.setOnClickListener(new BoxClickListener(2));
...
3 People found this is helpful
Advertisement