Question

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?

Was it helpful?

Solution

Assuming R.drawable.meore, R.drawable.mesame, etc. are drawable resources in your drawable folder, this should work.

public class Game extends Activity {

TextView result;
int[] drawableIds = {R.drawable.meore, R.drawablemeSame, R.drawable.meotxe, R.drawable.mexute, R.drawable.meeqvse, R.drawable.meshvide};   
int counter=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
  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);
  pirveli.setOnTouchListener(this);
}

public boolean onTouchEvent(MotionEvent event) {
  if(event.getAction()==MotionEvent.ACTION_DOWN){
     result.setText("Number of steps made: " + counter);
     counter++;
     pirveli.setImageResource( drawableIds[ drawableIds.length % counter ] );
  }
  return false;
}

Here, it will cycle through the resource Id's and change the ImageView every time the user touches it.

OTHER TIPS

First of all you should use the MotionEvent constants as they could change in future API versions, also you need to return true after a touch event is handled.

I also suggest you add null checks for good measure.

public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        if (result != null);
            result.setText("Number of steps made: " + counter);
        counter++; 

        if (pirveli != null)
            pirveli.setImageResource(R.drawable.meore);
        return true;
    }
    return false;
}

A stack trace would be of much assistance if none of those are your problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top