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()
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)