2010-07-11 47 views
0

所以每次我運行此代碼我的Android應用unexpectdly停止,我不明白爲什麼時間...Android應用程序總是意外停止,試圖做事件處理程序?

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


    }); 



    } 



} 
+0

查看來自Pentium10的關於如何調試Android應用的帖子 http://stackoverflow.com/questions/3222608/android-error-the-application-has-stopped-unexpectedly-please-try-again/3223115# 3223115 – ccheneson 2010-07-11 21:12:40

+0

你會看看DDMS和logcat來檢查問題http://goo.gl/i9by – 2010-07-11 21:21:10

+1

爲什麼一個按鈕的OnTouchListener而不是'OnClickListener'? – adamp 2010-07-11 22:54:39

回答

1

1的問題是,MotionEvent.ACTION_UPint類型,以便爲您的測試是正確的,你應該有

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

所有的OnTouchListener是不是真的,除非你一Button適當的聽衆都做的非常特殊的東西先。您應該爲您的Button實施OnClickListener

還考慮ccheneson發佈。你沒有正確比較它。