lundi 10 août 2015

Android: Some problems with if statement

I have a problem when I want to use if statement in my project I got error and I don't know how to fix this so the code is like this

int gamelifes=6; 

int  [] sixlifes ={R.drawable.lifessix};
int  [] fivelifes ={R.drawable.lifesfive};
int  [] fourlifes ={R.drawable.lifesfour};
int  [] threelifes ={R.drawable.lifesthree};
int  [] twolifes ={R.drawable.lifestwo};
int  [] onelifes ={R.drawable.lifesone};  

    lifes1 =(TextView)findViewById(R.id.lifestext1);
    lifes1.setTextColor(Color.RED);
    lifes1.setText(String.valueOf(gamelifes));

    lifesimage =(ImageView)findViewById(R.id.lifes1);
    lifesimage.setOnClickListener(this);

so the first problem is when I make if statement like this

    if(gamelifes[6]) 
         {          
        lifesimage.setImageResource(sixlifes[0]);           
         }
    if(gamelifes[5]) 
         {          
        lifesimage.setImageResource(fivelifes[0]);          
         }
    if(gamelifes[4]) 
         {          
        lifesimage.setImageResource(fourlifes[0]);          
         }

it says "The type of the expression must be an array type but it resolved to int"

then, I changed the code like this:

    if(gamelifes = 6) 
         {          
        lifesimage.setImageResource(sixlifes[0]);           
         }
    if(gamelifes = 5) 
         {          
        lifesimage.setImageResource(fivelifes[0]);          
         }
    if(gamelifes = 4) 
         {          
        lifesimage.setImageResource(fourlifes[0]);          
         }

it said "Type mismatch: cannot convert from int to boolean"

so I changed again the code like this:

    if(lifes1 = 6) 
         {          
        lifesimage.setImageResource(sixlifes[0]);           
         }
    if(lifes1 = 5) 
         {          
        lifesimage.setImageResource(fivelifes[0]);          
         }
    if(lifes1 = 4) 
         {          
        lifesimage.setImageResource(fourlifes[0]);          
         }

it still error "Type mismatch: cannot convert from int to TextView"

and, at last I changes the code like this

if(lifes1.setText("6")); 
         {          
        lifesimage.setImageResource(sixlifes[0]);           
         }
    if(lifes1.setText("5")); 
         {          
        lifesimage.setImageResource(fivelifes[0]);          
         }
    if(lifes1.setText("4"));
         {          
        lifesimage.setImageResource(fourlifes[0]);          
         }

it still error too and said "Type mismatch: cannot convert from void to boolean"

I was very confused. I have tried, but still more errors. can anyone help me how to fix this?

Aucun commentaire:

Enregistrer un commentaire