0

I'm making a strategic game, but its crashes all the time.

I'm using nine buttons to play the game. The buttons default color is grey and two players can play the game.

The first player can select three buttons (red) and the second player can select three buttons (green).

However, the problem is when I touch another button (grey) except above the six.

The app crashes, I don't know why. Code is attached below.

All button actions call the listed functions check() and moves()

enter image description here

b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        if(flag==0) {
            check(b1, color1);
        }
        moves(b1);
        //  Toast.makeText(getApplicationContext(),""+b1.getText(),Toast.LENGTH_LONG).show();

    }
});

The function:

void check(Button bn,int c)
{
        if (plyr == 1 && plyr1 >0&&c==Color.LTGRAY)
        {
            bn.setBackgroundColor(Color.RED);
            bn.setText("RED");
            plyr1--;
            plyr = 2;
            t1.setBackgroundColor(Color.LTGRAY);
            t2.setBackgroundColor(Color.GREEN);
        }

        else if (plyr == 2 && plyr2>0&&c==Color.LTGRAY)
        {
            bn.setBackgroundColor(Color.GREEN);
            bn.setText("GREEN");
            plyr2--;
            plyr = 1;
            t2.setBackgroundColor(Color.LTGRAY);
            t1.setBackgroundColor(Color.RED);

        }
        else if (plyr1 == 0 && plyr2 == 0)
        {
            flag = 1;
        }

}

void moves(Button bn)
{
    ColorDrawable btnclr=(ColorDrawable)bn.getBackground();
    int s=btnclr.getColor();

    if(s==Color.RED)
    {
        Toast.makeText(getApplicationContext(),"Exception GREEN",Toast.LENGTH_LONG).show();
    }
    else if(s==Color.LTGRAY)
    {

    }
    else
    {
        Toast.makeText(getApplicationContext(),"else",Toast.LENGTH_LONG).show();
    }

}
int adjsnt(Button adjlbl, Button rem)
{
    if(rem.getText()=="b2")


    {
        Toast.makeText(getApplicationContext(),"Exception",Toast.LENGTH_LONG).show();
        return 1;}
    return 0;
}

log cat

java.lang.ClassCastException: android.graphics.drawable.PaintDrawable cannot be cast to android.graphics.drawable.ColorDrawable at com.solutions.techblaze.nera.Nera_main.moves(Nera_main.java:250) at com.solutions.techblaze.nera.Nera_main$8.onClick(Nera_main.java:171) at android.view.View.performClick(View.java:4856) at android.view.View$PerformClick.run(View.java:19956) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5389) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

unniraja
  • 27
  • 5

1 Answers1

2

As the log says PaintDrawable cannot be cast to ColorDrawable.

When you're doing ColorDrawable btnclr=(ColorDrawable)bn.getBackground(); you're assuming that the result of getBackground is a ColorDrawable. But in this case it's not. This is probably because you haven't set a color with setBackgroundColor on those views.

One thing you can do is to simple check if the object that getBackground returns is an instance of ColorDrawable before casting it:

if(bn.getBackground() instanceof ColorDrawable)

Another thing you can do is to set the color on runtime to all your views. setBackgroundColor will always set a ColorDrawable to your views.

Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82
  • No problem friend. Remember to mark the answer as correct if it solved your problem or vote up if it helped you somehow. Have a nice day :) – Pedro Oliveira Jan 21 '16 at 16:35