I’m a beginner in android and at the moment, I’m working on simple app. I have 7 pictures and I want to change one-by-one when user touches the screen. Here’s my code, my app crashes on the second touch on the screen. If anyone can help, I’d be thankful.
public class Game extends Activity {
public TextView result;
public ImageView pirveli, meore, mesame, meotxe, mexute, meeqvse, meshvide;
int counter=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
result = (TextView) findViewById(R.id.result);
result.setText("Number of steps made: " + counter);
pirveli = (ImageView) findViewById(R.drawable.pirveli);
meore = (ImageView) findViewById(R.drawable.meore);
mesame = (ImageView) findViewById(R.drawable.mesame);
meotxe = (ImageView) findViewById(R.drawable.meotxe);
mexute = (ImageView) findViewById(R.drawable.mexute);
meeqvse = (ImageView) findViewById(R.drawable.meeqvse);
meshvide= (ImageView) findViewById(R.drawable.meshvide);
}
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==0){
result.setText("Number of steps made: " + counter);
counter++;
pirveli.setImageResource(R.drawable.meore);
}
return false;
}
AND one more question: when I touch the screen the first time, int counter isn’t increasing. how can i fix it?
Assuming
R.drawable.meore,R.drawable.mesame, etc. are drawable resources in yourdrawablefolder, this should work.Here, it will cycle through the resource Id’s and change the
ImageViewevery time the user touches it.