views:

34

answers:

1

So every time I run this code my Android app stops unexpectdly, and i dont get why...

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;


public class TheStupidTest extends Activity {


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final TextView text1 = (TextView) findViewById(R.id.TextView01);
    text1.setText("well this works at least");

    Button yButton = (Button) findViewById(R.id.button_yellow);
    yButton.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if ( event.equals(MotionEvent.ACTION_UP) ) { 
                text1.setText("You pressed the yellow button"); 
                return true; 
            } 

            return false;
        }


    });



    } 



}
A: 

1 problem is that MotionEvent.ACTION_UP is of type int so for your test to be correct, you should have

if ( event.getAction() == MotionEvent.ACTION_UP) {
ccheneson