2013-02-24 107 views
0

我試圖編輯在點擊一個按鈕TextView的... 這是代碼爲什麼我的應用程序崩潰?

TextView 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:editable="true"> 
/TextView> 
Button 
    android:id="@+id/b1" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="50dp" 
    android:text="1" 
    android:onClick="write" /> 

的XML部分單擊按鈕時寫功能已... 我的應用程序崩潰,只要叫這個寫函數被調用......

public void write(){ 
    TextView text=(TextView)findViewById(R.id.editText1); 
    text.setText(Integer.toString(1)); 
} 

回答

4

嘗試這樣的事情

public void write(View view){ 
TextView text=(TextView)view; // can do like this instead of findViewById 
text.setText(Integer.toString(1)); 
} 

由於@Doomskni ght評論了

即使未使用,您的方法write也需要View參數。否則它不會找到你正在嘗試呼叫的方法。

+0

是的。它需要View參數,即使它沒有被使用。否則它不會找到你正在嘗試呼叫的方法。 +1 – Doomsknight 2013-02-24 10:24:12

相關問題