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;
impor开发者_StackOverflow中文版t 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;
}
});
}
}
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) {
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.
精彩评论