Will
Will

Reputation: 1

Android app always stops unexpectedly, trying to do event handler?

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;
        }


    });



    } 



}

Upvotes: 0

Views: 361

Answers (2)

Octavian Helm
Octavian Helm

Reputation: 39604

First of all an OnTouchListener is not really the appropriate listener for a Button except you are doing very special things. You should implement an OnClickListener for your Button.

And also consider what ccheneson posted. You are not comparing it correctly.

Upvotes: 0

ccheneson
ccheneson

Reputation: 49410

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

Upvotes: 1

Related Questions